Interrompere la conversione o il caricamento utilizzando InterruptMonitor quando ci vuole troppo tempo

Possibili Scenari di Utilizzo

Aspose.Cells ti consente di interrompere la conversione del Workbook in vari formati come PDF, HTML ecc. utilizzando l’oggetto InterruptMonitor quando richiede troppo tempo. Il processo di conversione è spesso intensivo sia per la CPU che per la memoria ed è utile interromperlo quando le risorse sono limitate. Puoi usare InterruptMonitor sia per interrompere la conversione che per impedire il caricamento di un ampio workbook. Usa la proprietà Workbook.InterruptMonitor per interrompere la conversione e la proprietà LoadOptions.InterruptMonitor per il caricamento di un ampio workbook.

Interrompere la conversione o il caricamento utilizzando InterruptMonitor quando sta impiegando troppo tempo

Il seguente codice di esempio illustra l’uso dell’oggetto InterruptMonitor. Il codice converte un file Excel abbastanza grande in PDF. Ci vorranno diversi secondi (più di 30 secondi) per completare la conversione a causa di queste righe di codice.

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

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

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

Come si può vedere J1000000 è una cella piuttosto lontana nel file XLSX. Tuttavia, il metodo WaitForWhileAndThenInterrupt() interrompe la conversione dopo 10 secondi e il programma termina. Utilizzare il seguente codice per eseguire il codice di esempio.

 new StopConversionOrLoadingUsingInterruptMonitor().TestRun();

Codice di Esempio

// 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.");
}
}