أوقف التحويل أو التحميل باستخدام InterruptMonitor عندما يستغرق وقتًا طويلاً

سيناريوهات الاستخدام الممكنة

يسمح لك Aspose.Cells بإيقاف تحويل المصنف إلى تنسيقات مختلفة مثل PDF و HTML وما إلى ذلك باستخدامInterruptMonitor كائن عندما يستغرق وقتا طويلا. غالبًا ما تكون عملية التحويل كثيفة الاستخدام لوحدة المعالجة المركزية والذاكرة وغالبًا ما يكون من المفيد إيقافها عندما تكون الموارد محدودة. يمكنك استخدامInterruptMonitorسواء لإيقاف التحويل وكذلك لإيقاف تحميل مصنف ضخم. الرجاء استخدامالمصنف خاصية وقف التحويل وLoadOptions.InterruptMonitor خاصية تحميل مصنف ضخم.

أوقف التحويل أو التحميل باستخدام InterruptMonitor عندما يستغرق وقتًا طويلاً

يشرح نموذج التعليمات البرمجية التالي استخدامInterruptMonitor موضوع. يقوم الكود بتحويل ملف Excel كبير جدًا إلى 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.");
}
}