Konvertierung oder Laden mit InterruptMonitor stoppen, wenn es zu lange dauert
Mögliche Verwendungsszenarien
Aspose.Cells ermöglicht es Ihnen, die Konvertierung einer Arbeitsmappe in verschiedene Formate wie PDF, HTML usw. mit dem InterruptMonitor-Objekt zu stoppen, wenn es zu lange dauert. Der Konvertierungsprozess ist oft sowohl rechen- als auch speicherintensiv und es ist oft nützlich, ihn zu stoppen, wenn die Ressourcen begrenzt sind. Sie können InterruptMonitor sowohl zum Stoppen der Konvertierung als auch zum Stoppen des Ladens großer Arbeitsmappen verwenden. Verwenden Sie das Workbook.InterruptMonitor-Eigenschaft zum Stoppen der Konvertierung und das LoadOptions.InterruptMonitor-Eigenschaft zum Laden großer Arbeitsmappen.
** Konvertierung oder Laden mit InterruptMonitor stoppen, wenn es zu lange dauert**
Der folgende Beispielscode erläutert die Verwendung des InterruptMonitor-Objekts. Der Code konvertiert eine ziemlich große Excel-Datei in PDF. Es dauert einige Sekunden (d.h. mehr als 30 Sekunden), um sie zu konvertieren, aufgrund dieser Codezeilen.
//Access cell J1000000 and add some text inside it.
Cell cell = ws.Cells["J1000000"];
cell.PutValue("This is text.");
Wie Sie sehen, ist J1000000 in der XLSX-Datei recht weit entfernt. Die Methode WaitForWhileAndThenInterrupt() unterbricht die Konvertierung nach 10 Sekunden und das Programm endet/terminiert. Verwenden Sie bitte den folgenden Code, um den Beispielscode 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."); | |
} | |
} |