Çok uzun sürüyorsa, Durdur dönüşümü veya yüklemeyi kullanarak InterruptMonitor

Olası Kullanım Senaryoları

Aspose.Cells, kaynakların sınırlı olduğu durumlarda dönüşüm sürecini durdurmaya yarayan InterruptMonitor nesnesini kullanmanıza izin verir. Dönüşüm süreci genellikle hem CPU hem de Bellek yoğun olup, kaynaklar sınırlı olduğunda durdurmak yararlı olabilir. Dönüşümü durdurmak için InterruptMonitor hem dönüşümü durdurmak hem de büyük çalışma kitabını yüklemeyi durdurmak için kullanabilirsiniz. Dönüşümü durdurmak için Workbook.InterruptMonitor özelliğini ve büyük çalışma kitabını yüklemek için LoadOptions.InterruptMonitor özelliğini kullanabilirsiniz.

Çok uzun sürüyorsa, Duraklatma İzleyiciyi kullanarak dönüşümü veya yüklemeyi durdurun

Aşağıdaki örnek kod, InterruptMonitor nesnesinin kullanımını açıklar. Kod, oldukça büyük bir Excel dosyasını PDF’e dönüştürür. Bu kod satırları nedeniyle dönüştürülmesi birkaç saniye (yani, 30 saniyeden fazla) sürecektir.

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

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

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

Gördüğünüz gibi J1000000 XLSX dosyasındaki oldukça uzağa bir hücredir. Ancak WaitForWhileAndThenInterrupt() metodu 10 saniye sonra dönüşümü keser ve program sona erer/sonlandırır. Örnek kodu çalıştırmak için lütfen aşağıdaki kodu kullanın.

 new StopConversionOrLoadingUsingInterruptMonitor().TestRun();

Örnek Kod

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