如何在JavaScript中的线程之间进行通信?

时间:2021-10-30 20:59:47

I created an XPCOM object in C++ for a FireFox extension. I'm using a worker thread to listen for an event and when it happens, I need to do stuff on the main thread. Obviously, I can't just sit and wait in JavaScript on the main thread because you need to be able to use the browser (my event happens very rarely). I tried doing this in the thread (xpcom guy sends javascript an event): window.setTimeout( myImportantWorkFunction, 100 );

我在C ++中为FireFox扩展创建了一个XPCOM对象。我正在使用工作线程来监听事件,当它发生时,我需要在主线程上做一些事情。显然,我不能只是坐在主线程上等待JavaScript,因为你需要能够使用浏览器(我的事件很少发生)。我尝试在线程中执行此操作(xpcom man发送javascript事件):window.setTimeout(myImportantWorkFunction,100);

This works (on the main thread, as intended), but it will pause indefinately; it doesn't happen after 100ms as intended. You have to click around a bit or resize the window and then suddenly the function gets called. Like JavaScript suddently woke up. I assume this is because it's happening in a thread.

这有效(在主线程上,如预期的那样),但它将无限期地暂停;它不会在100毫秒后发生。你必须点击一下或调整窗口大小,然后突然调用该函数。就像JavaScript突然醒来一样。我认为这是因为它发生在一个线程中。

Is there some better way for the worker to ask the main thread to do something?

是否有更好的方法让工人要求主线程做某事?

3 个解决方案

#1


JavaScript only has one thread. Function calls always block until they return. If you are communicating from JS to the browser (or extension of the browser in this case), you should make sure your browser-side code returns to the JS immediately, and remember a callback to invoke when your work completes (this is how setTimeout works).

JavaScript只有一个线程。函数调用总是阻塞,直到它们返回。如果您正在从JS与浏览器(或本例中的浏览器扩展)进行通信,则应确保您的浏览器端代码立即返回到JS,并记住在您的工作完成时调用的回调(这是setTimeout的方法)作品)。

I suspect the "clicking around" is just a coincidence. Have you tried alerting as soon as the event is fired?

我怀疑“点击”只是巧合。一旦事件被解雇,您是否尝试过警报?

#2


For those that care, I gave up on trying to message between threads. I found a way to compile the XPCOM object with some objective-C++ so that I could use their NSDistributedNotificationCenter. This lets me get my event on the main thread where javascript is happy.

对于那些关心的人,我放弃了尝试在线程之间发送消息。我找到了一种用一些Objective-C ++编译XPCOM对象的方法,这样我就可以使用它们的NSDistributedNotificationCenter。这让我可以在javascript很开心的主线程上获取我的活动。

The question is still valid but I probably won't take the time to validate anybody's answer now...

问题仍然有效,但我现在可能不会花时间验证任何人的答案......

#3


Did you try https://developer.mozilla.org/en/nsISupports_proxies?

您是否尝试过https://developer.mozilla.org/en/nsISupports_proxies?

#1


JavaScript only has one thread. Function calls always block until they return. If you are communicating from JS to the browser (or extension of the browser in this case), you should make sure your browser-side code returns to the JS immediately, and remember a callback to invoke when your work completes (this is how setTimeout works).

JavaScript只有一个线程。函数调用总是阻塞,直到它们返回。如果您正在从JS与浏览器(或本例中的浏览器扩展)进行通信,则应确保您的浏览器端代码立即返回到JS,并记住在您的工作完成时调用的回调(这是setTimeout的方法)作品)。

I suspect the "clicking around" is just a coincidence. Have you tried alerting as soon as the event is fired?

我怀疑“点击”只是巧合。一旦事件被解雇,您是否尝试过警报?

#2


For those that care, I gave up on trying to message between threads. I found a way to compile the XPCOM object with some objective-C++ so that I could use their NSDistributedNotificationCenter. This lets me get my event on the main thread where javascript is happy.

对于那些关心的人,我放弃了尝试在线程之间发送消息。我找到了一种用一些Objective-C ++编译XPCOM对象的方法,这样我就可以使用它们的NSDistributedNotificationCenter。这让我可以在javascript很开心的主线程上获取我的活动。

The question is still valid but I probably won't take the time to validate anybody's answer now...

问题仍然有效,但我现在可能不会花时间验证任何人的答案......

#3


Did you try https://developer.mozilla.org/en/nsISupports_proxies?

您是否尝试过https://developer.mozilla.org/en/nsISupports_proxies?