长时间操作导致Office外挂程式(JS)崩溃 [英] Long operations crash Office addin (JS)
问题描述
我刚刚创建了(JS)Word加载项,发现长时间的同步操作会使它崩溃.在这些情况下,将显示以下错误-[ADD-IN ERROR抱歉,我们必须重新启动,因为此加载项没有响应.]
I just created a (JS) Word Add-in and found that long synchronous operations can make it crash. In these cases, the following error is displayed - [ADD-IN ERROR Sorry, we had to restart because this add-in wasn't responding.]
在单击按钮时运行以下代码.
The following code is ran on a button click.
function scanText() {
Word.run(function (context) {
var body = context.document.body;
context.load(body, 'text');
return context.sync().then(function () {
var r = thisOperationCanTakeALongTimeIfDocIsLarge(body.text);
});
})
.catch(errorHandler);
}
如何防止这种情况发生?我应该使长时间操作异步吗?在这种情况下如何实现?
How do I prevent this from happening? should I make the long operation asynchronous? How is this achieved in this context?
推荐答案
我终于找到了解决此问题的好方法...我像这样使用WebWorker:
I have finally found a good way to solve this... I use a WebWorker like so:
function scanText() {
var w;
if (typeof (w) == "undefined") {
w = new Worker("./Scripts/myWebWorker.js");
}
else
{
showNotification("Sorry! No Web Worker support.");
}
w.onmessage = function (event) {
showNotification(event.data);
};
Word.run(function (context) {
var body = context.document.body;
context.load(body, 'text');
return context.sync().then(function () {
w.postMessage(body.text);
});
})
.catch(errorHandler);
}
以及myWebWorker.js文件:
And the myWebWorker.js file:
self.importScripts([...some scripts i need...]);
self.addEventListener("message", function (e) {
var r = thisOperationCanTakeALongTimeIfDocIsLarge(e.data);
postMessage(r);
}, false);
这篇关于长时间操作导致Office外挂程式(JS)崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!