توقّف عن التحويل أو التحميل باستخدام InterruptMonitor عندما يستغرق وقتًا طويلاً
سيناريوهات الاستخدام المحتملة
Aspose.Cells يسمح لك بإيقاف تحويل جدول العمل إلى تنسيقات مختلفة مثل PDF، HTML وما إلى ذلك باستخدام كائن InterruptMonitor عندما يستغرق الأمر وقتًا طويلاً. يكون عملية التحويل مكثفة للكمبيوتر والذاكرة ومن المفيد في كثير من الأحيان إيقافها عندما تكون الموارد محدودة. يمكنك استخدام InterruptMonitor كل من لإيقاف التحويل وكذلك لإيقاف تحميل جدول عمل ضخم. يرجى استخدام خاصية Workbook.InterruptMonitor لإيقاف التحويل وخاصية LoadOptions.InterruptMonitor لتحميل جدول العمل الضخم.
توقّف عن التحويل أو التحميل باستخدام InterruptMonitor عندما يستغرق وقتًا طويلاً
يشرح الكود العينة التالي استخدام كائن InterruptMonitor. يحول الكود ملف إكسل كبير إلى PDF. سيستغرق عدة ثوانٍ (أي أكثر من 30 ثانية) لتحويله بسبب هذه الأسطر من الكود.
//Access cell J1000000 and add some text inside it.
Cell cell = ws.Cells["J1000000"];
cell.PutValue("This is text.");
كما ترون J1000000 هي خلية بعيدة جدًا في ملف XLSX. ومع ذلك، يقوم طريقة WaitForWhileAndThenInterrupt() بإيقاف التحويل بعد 10 ثوانٍ وينتهي/ينقطع البرنامج. يرجى استخدام الكود التالي لتنفيذ الكود العينة.
new StopConversionOrLoadingUsingInterruptMonitor().TestRun();
الكود المثالي
// 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."); | |
} | |
} |