1

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 when 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

sgres
  • 11
  • 1

1 Answers1

1

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});
Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • alert doesnt work (since it is not thread safe). Is there another way to output besides throw-ing and console.log? – user654628 Jul 02 '11 at 22:16
  • @user654628: That's code running on the main thread - so `alert` does work. Anyway, that's just an example, nothing like the code you would really use. – Wladimir Palant Jul 02 '11 at 22:35