Çevrimi durdurmak veya Node.js ve C++ ile çok uzun sürüyorsa yüklemeyi InterruptMonitor kullanarak durdurun

Olası Kullanım Senaryoları

Aspose.Cells for Node.js via C++, çok uzun sürdüğü durumlarda InterruptMonitor nesnesini kullanarak WorkBook’un çeşitli formatlara (PDF, HTML vb.) dönüştürülmesini durdurmanıza izin verir. Dönüştürme işlemi genellikle CPU ve Bellek açısından yoğundur ve kaynaklar kısıtlıysa durdurmak faydalı olur. Hem dönüştürmeyi durdurmak hem de büyük boyutlu çalışma kitabı yüklemeyi durdurmak için InterruptMonitor kullanabilirsiniz. Lütfen durdurma işlemi için Workbook.getInterruptMonitor() özelliğini, büyük boyutlu çalışma kitabını yüklemek için ise LoadOptions.getInterruptMonitor() özelliğini kullanın.

Ç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.

let cell = ws.getCells().get("J1000000");

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

Gördüğünüz gibi J1000000 XLSX dosyasında oldukça uzak bir hücredir. Ancak, waitForWhileAndThenInterrupt() yöntemi dönüşümü 10 saniye sonra durdurur ve program sona erer/sonlandırılır. Lütfen örnek kodu çalıştırmak için aşağıdaki kodu kullanın.

new StopConversionOrLoadingUsingInterruptMonitor().testRun();

Örnek Kod

const AsposeCells = require("aspose.cells.node");
const path = require("path");

class StopConversionOrLoadingUsingInterruptMonitor {
// Output directory
static outputDir = StopConversionOrLoadingUsingInterruptMonitor.getOutputDirectory();

constructor() {
// Create InterruptMonitor object
this.im = new AsposeCells.InterruptMonitor();
}

// This function will create workbook and convert it to Pdf format
async createWorkbookAndConvertItToPdfFormat(threadObj) {
const monitorThread = threadObj;

// Create a workbook object
const wb = new AsposeCells.Workbook();

// Assign it InterruptMonitor object
wb.setInterruptMonitor(this.im);

// Access first worksheet
const ws = wb.getWorksheets().get(0);

// Access cell J1000000 and add some text inside it.
const cell = ws.getCells().get("J1000000");
cell.putValue("This is text.");

try {
// Save the workbook to Pdf format
await wb.saveAsync(path.join(StopConversionOrLoadingUsingInterruptMonitor.outputDir, "output_InterruptMonitor.pdf"));

// Show successful message
console.log("Excel to PDF - Successful Conversion");

// Stop monitor thread
monitorThread.interrupt();
} catch (ex) {
if (ex.code === AsposeCells.ExceptionType.Interrupted) {
console.log("Conversion process is interrupted - Message: " + ex.message);
} else {
throw ex;
}
}
}

// This function will interrupt the conversion process after 10s
waitForWhileAndThenInterrupt() {
try {
setTimeout(() => {
this.im.interrupt();
}, 1000 * 10);
} catch (e) {
console.log("Monitor thread is interrupted - Message: " + e.message);
}
}

async testRun() {
const monitorThread = new Promise((resolve) => {
this.waitForWhileAndThenInterrupt();
resolve();