监控运行中的程序

如何监控运行中的程序

以下示例代码显示了如何监控运行中的程序。此代码可用于监控与Workbook相关的代码运行。只需使用SystemTimeInterruptMonitor类创建一个监控对象,使用SetInterruptMonitor函数将其添加到LoadOptions运行参数中,然后使用StartMonitor函数设置预期中断时间(以毫秒为单位)。如果受监视代码的运行时间超过预期时间,程序将被中断并抛出异常。

示例代码

SystemTimeInterruptMonitor monitor = new SystemTimeInterruptMonitor(false);
LoadOptions lopts = new LoadOptions();
lopts.setInterruptMonitor(monitor);
monitor.startMonitor(1000); //time limit is 1 second
Workbook wb = new Workbook("Large.xlsx", lopts);
//if the time cost of loading the template file exceeds one second, interruption will be required and exception will be thrown here
//otherwise starts to monitor the save procedure
monitor.startMonitor(1500); //time limit is 1.5 seconds
wb.save("result.xlsx");