当转换或加载时间过长時,使用 InterruptMonitor 停止转换或加载,支持 C++
可能的使用场景
Aspose.Cells 允许在转换到PDF、HTML等格式时,当过程耗时过长,可以使用 InterruptMonitor 对象停止转换。转换过程常常占用大量CPU和内存,在资源有限时,暂停操作非常有用。可以使用 InterruptMonitor 来停止转换,也可以用来停止加载大型工作簿。请使用 Workbook.InterruptMonitor 属性停止转换,LoadOptions.InterruptMonitor 属性停止加载大型工作簿。
在转换或加载花费太长时间时使用InterruptMonitor停止转换或加载
以下示例代码说明如何使用 InterruptMonitor 对象。代码将一个较大的Excel文件转换为PDF,预计需要几秒(即超过30秒)才能完成。
// Access cell J1000000 and add some text inside it.
auto cell = ws->GetCells()->GetObjectByIndex("J1000000");
cell->PutValue("This is text.");
如您所见,J1000000 在XLSX文件中是一个相当远的单元格。然而,WaitForWhileAndThenInterrupt() 方法在10秒后中断了转换,程序终止。请使用以下代码执行示例。
new StopConversionOrLoadingUsingInterruptMonitor().TestRun();
示例代码
#include <iostream>
#include <thread>
#include <chrono>
#include <atomic>
#include <memory>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace std;
class StopConversionOrLoadingUsingInterruptMonitor
{
private:
// Output directory
static U16String outputDir;
// Create InterruptMonitor object
InterruptMonitor im;
std::atomic<bool> conversionCompleted{false};
// This function will create workbook and convert it to Pdf format
void CreateWorkbookAndConvertItToPdfFormat()
{
// Create a workbook object
Workbook wb;
// Assign it InterruptMonitor object
wb.SetInterruptMonitor(im);
// Access first worksheet
Worksheet ws = wb.GetWorksheets().Get(0);
// Access cell J1000000 and add some text inside it.
Cell cell = ws.GetCells().Get(u"J1000000");
cell.PutValue(u"This is text.");
try
{
// Save the workbook to Pdf format
wb.Save(outputDir + u"output_InterruptMonitor.pdf");
// Show successful message
cout << "Excel to PDF - Successful Conversion" << endl;
conversionCompleted = true;
}
catch (CellsException& ex)
{
if (ex.GetCode() == ExceptionType::Interrupted)
{
cout << "Conversion process is interrupted - Message: " << ex.GetErrorMessage().ToUtf8() << endl;
}
else
{
throw;
}
}
}
// This function will interrupt the conversion process after 10s
void WaitForWhileAndThenInterrupt()
{
auto start = chrono::steady_clock::now();
while (chrono::steady_clock::now() - start < chrono::seconds(10))
{
if (conversionCompleted.load())
return;
this_thread::sleep_for(chrono::milliseconds(100));
}
im.Interrupt();
}
public:
void TestRun()
{
thread monitorThread(&StopConversionOrLoadingUsingInterruptMonitor::WaitForWhileAndThenInterrupt, this);
thread conversionThread(&StopConversionOrLoadingUsingInterruptMonitor::CreateWorkbookAndConvertItToPdfFormat, this);
monitorThread.join();
conversionThread.join();
}
static void Run()
{
StopConversionOrLoadingUsingInterruptMonitor instance;
instance.TestRun();
cout << "StopConversionOrLoadingUsingInterruptMonitor executed successfully." << endl;
}
};
U16String StopConversionOrLoadingUsingInterruptMonitor::outputDir = u"..\\Data\\02_OutputDirectory\\";
int main()
{
Aspose::Cells::Startup();
StopConversionOrLoadingUsingInterruptMonitor::Run();
Aspose::Cells::Cleanup();
return 0;
}