時間がかかりすぎる場合はInterruptMonitorを使用して変換または読み込みを停止してください
可能な使用シナリオ
Aspose.Cellsを使用すると、リソースが限られている場合に変換を停止するために、InterruptMonitorオブジェクトを使用できます。変換プロセスはCPUおよびメモリの両方を多く使用するため、リソースが制限されているときに中断すると便利です。変換と巨大なワークブックの読み込みの両方を停止するために、InterruptMonitorを使用できます。変換を中止する場合はWorkbook.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."); | |
} | |
} |