Stop conversion or loading using InterruptMonitor when it is taking too long
Possible Usage Scenarios
Aspose.Cells allows you to stop the conversion of Workbook to various formats like PDF, HTML etc. using the InterruptMonitor object when it is taking too long. The conversion process is often both CPU and Memory intensive and it is often useful to halt it when resources are limited. You can use InterruptMonitor both for stopping conversion as well as to stop loading huge workbook. Please use Workbook.InterruptMonitor property for stopping conversion and LoadOptions.InterruptMonitor property for loading huge workbook.
Stop conversion or loading using InterruptMonitor when it is taking too long
The following sample code explains the usage of InterruptMonitor object. The code converts quite a large Excel file to PDF. It will take several seconds (i.e. more than 30 seconds) to get it converted because of these lines of code.
//Access cell J1000000 and add some text inside it.
Cell cell = ws.Cells["J1000000"];
cell.PutValue("This is text.");
As you see J1000000 is quite a farther cell in XLSX file. However, the WaitForWhileAndThenInterrupt() method interrupts the conversion after 10 seconds and program ends/terminates. Please use the following code to execute the sample code.
new StopConversionOrLoadingUsingInterruptMonitor().TestRun();
Sample Code
// 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."); | |
} | |
} |