Arrêter la conversion ou le chargement à l aide d InterruptMonitor lorsqu elle prend trop de temps
Scénarios d’utilisation possibles
Aspose.Cells vous permet d’arrêter la conversion du classeur à divers formats tels que PDF, HTML, etc. en utilisant l’objet InterruptMonitor lorsqu’elle prend trop de temps. Le processus de conversion est souvent intensif en CPU et en mémoire, et il est souvent utile de l’arrêter lorsque les ressources sont limitées. Vous pouvez utiliser InterruptMonitor à la fois pour arrêter la conversion et pour arrêter le chargement d’un gros classeur. Veuillez utiliser la propriété Workbook.InterruptMonitor pour arrêter la conversion et la propriété LoadOptions.InterruptMonitor pour charger un gros classeur.
Arrêter la conversion ou le chargement à l’aide de InterruptMonitor lorsqu’il prend trop de temps
Le code d’exemple suivant explique l’utilisation de l’objet InterruptMonitor. Le code convertit un fichier Excel assez volumineux en PDF. Cela prendra plusieurs secondes (c’est-à-dire plus de 30 secondes) pour le convertir en raison de ces lignes de code.
//Access cell J1000000 and add some text inside it.
Cell cell = ws.Cells["J1000000"];
cell.PutValue("This is text.");
Comme vous pouvez le constater, J1000000 est une cellule assez éloignée dans le fichier XLSX. Cependant, la méthode WaitForWhileAndThenInterrupt() interrompt la conversion après 10 secondes et le programme se termine/se termine. Veuillez utiliser le code suivant pour exécuter le code d’exemple.
new StopConversionOrLoadingUsingInterruptMonitor().TestRun();
Code d’exemple
// 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."); | |
} | |
} |