[Project_owners] Threads in mozilla

Neil neil at parkwaycc.co.uk
Tue Jan 27 00:11:19 EST 2004

Davide Carboni wrote:

> Hi,
> I successfully deployed a code like this under xpcshell:
> function startThread(runnable){
>    var 
> threadclass=Components.classes["@mozilla.org/thread;1"].getService(Components.interfaces.nsIThread);
>    threadclass.init(runnable,512*1024,
>            Components.interfaces.nsIThread.PRIORITY_NORMAL,
>            Components.interfaces.nsIThread.SCOPE_LOCAL,
>            Components.interfaces.nsIThread.STATE_JOINABLE);
> }
> where runnable implements a method Run() which loops for ever and does 
> something useful for me!! The thread loops in background as forked 
> from the shell. The problem is that the same function invoked as 
> script from a XUL component in Mozilla causes the browser to hang 
> without executing the code of method Run()
> If the script directly invokes runnable.Run()  the method is executed 
> (I can see the results) but the system hangs anyway
> I'm missing something, can anyone explain such a behaviour. I expected 
> that threadclass.init(...) would fork a new thread, at least this is 
> the behaviour in xpcshell.

If your runnable is written in script then this won't work because in 
Mozilla all script is proxied back on to the UI thread. Sorry, but you 
have to fiddle with setTimeout/setInterval/nsITimer :-(

More information about the Project_owners mailing list