Progress Meter and XPCOM
I'm developing a Firefox extension that uses PyXPCOM to run a process. I would like to have a progress meter that is shown when the process is started and gives feedback to the user.
In the javascript I have called the Thread Manager to run the process in Python:
var threadManager = Components.classes["@mozilla.org/thread-manager;1"].getService();
var background = threadManager.newThread(0);
background.dispatch(obj, background.DISPATCH_NORMAL);
so I wonder whether there is a way to check when the thread starts its job and w开发者_运维技巧hen it finishes. This helps me to control my progress meter in javascript!
If anyone has better idea in implementing the progress meter, please let me know :)
Thanks
You shouldn't be creating new threads from JavaScript directly - this has lots of thread safety issues and from all I know this functionality is no longer available in Firefox 4. The replacement are chrome workers: https://developer.mozilla.org/en/DOM/ChromeWorker. So you would create your worker like this:
var worker = new ChromeWorker("script.js");
worker.postMessage(obj);
You would also want to receive messages from the worker (e.g. progress notifications).
worker.onmessage = function(event)
{
if (event.data.type == "progress")
alert("Worker progress: " + event.data.value);
else if (event.data.type == "done")
alert("Worker done!");
}
The worker itself would need to send you progress notifications via postMessage
function of course, e.g.:
postMessage({type: "progress", value: done/total*100});
精彩评论