Stoppen Sie die Konvertierung oder das Laden mit InterruptMonitor, wenn es zu lange dauert

Mögliche Nutzungsszenarien

Aspose.Cells ermöglicht es Ihnen, die Konvertierung der Arbeitsmappe in verschiedene Formate wie PDF, HTML usw. zu stoppenInterruptMonitor Objekt, wenn es zu lange dauert. Der Konvertierungsprozess ist häufig sowohl CPU- als auch speicherintensiv und es ist oft sinnvoll, ihn anzuhalten, wenn die Ressourcen begrenzt sind. Sie können verwendenInterruptMonitorsowohl zum Stoppen der Konvertierung als auch zum Stoppen des Ladens riesiger Arbeitsmappen. Bitte verwendeWorkbook.InterruptMonitor Eigenschaft zum Stoppen der Konvertierung undLoadOptions.InterruptMonitor Eigenschaft zum Laden einer riesigen Arbeitsmappe.

Stoppen Sie die Konvertierung oder das Laden mit InterruptMonitor, wenn es zu lange dauert

Der folgende Beispielcode erläutert die Verwendung vonInterruptMonitor Objekt. Der Code konvertiert eine ziemlich große Excel-Datei in PDF. Es dauert einige Sekunden (dhmehr als 30 Sekunden), um es aufgrund dieser Codezeilen konvertieren zu lassen.

//Access cell J1000000 and add some text inside it.

Cell cell = ws.Cells["J1000000"];

cell.PutValue("This is text.");

Wie du siehstJ1000000 ist eine ziemlich weiter entfernte Zelle in der Datei XLSX. Allerdings ist die**WaitForWhileAndThenInterrupt()**Methode unterbricht die Konvertierung nach 10 Sekunden und Programm endet/beendet. Bitte verwenden Sie den folgenden Code, um den Beispielcode auszuführen.

 new StopConversionOrLoadingUsingInterruptMonitor().TestRun();

Beispielcode

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
public class StopConversionOrLoadingUsingInterruptMonitor
{
//Output directory
static string outputDir = RunExamples.Get_OutputDirectory();
//Create InterruptMonitor object
InterruptMonitor im = new InterruptMonitor();
//This function will create workbook and convert it to Pdf format
void CreateWorkbookAndConvertItToPdfFormat(object threadObj)
{
Thread monitorThread = (Thread)threadObj;
//Create a workbook object
Workbook wb = new Workbook();
//Assign it InterruptMonitor object
wb.InterruptMonitor = im;
//Access first worksheet
Worksheet ws = wb.Worksheets[0];
//Access cell J1000000 and add some text inside it.
Cell cell = ws.Cells["J1000000"];
cell.PutValue("This is text.");
try
{
//Save the workbook to Pdf format
wb.Save(outputDir + "output_InterruptMonitor.pdf");
//Show successful message
Console.WriteLine("Excel to PDF - Successful Conversion");
//stop monitor thread
monitorThread.Interrupt();
}
catch (Aspose.Cells.CellsException ex)
{
if (ex.Code == ExceptionType.Interrupted)
{
Console.WriteLine("Conversion process is interrupted - Message: " + ex.Message);
}
else
{
throw ex;
}
}
}
//This function will interrupt the conversion process after 10s
void WaitForWhileAndThenInterrupt()
{
try
{
Thread.Sleep(1000 * 10);
im.Interrupt();
}
catch(ThreadInterruptedException e)
{
Console.WriteLine("Monitor thread is interrupted - Message: " + e.Message);
}
}
public void TestRun()
{
Thread monitorThread = new Thread(WaitForWhileAndThenInterrupt);
Thread conversionThread = new Thread(CreateWorkbookAndConvertItToPdfFormat);
monitorThread.Start();
conversionThread.Start(monitorThread);
monitorThread.Join();
conversionThread.Join();
}
public static void Run()
{
new StopConversionOrLoadingUsingInterruptMonitor().TestRun();
Console.WriteLine("StopConversionOrLoadingUsingInterruptMonitor executed successfully.");
}
}