Browser freeze with extension - Mozilla

This is a discussion on Browser freeze with extension - Mozilla ; Hello, I am working on an extension that allow easy web form login (a king of password manager). During the execution of my javascript, I have somewhere a function that takes several seconds to return a value: var result = ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Browser freeze with extension

  1. Browser freeze with extension

    Hello,

    I am working on an extension that allow easy web form login (a king of
    password manager).

    During the execution of my javascript, I have somewhere a function
    that takes several seconds to return a value:

    var result = myComponent.myFunction();
    if(result ==1)
    {
    ....
    }

    During this time, the browser is freezing (for example, I can't change
    tab during this time). Is there a way to run this function in a way
    that the browser is not freezing?

    Thanks in advance for your help

  2. Re: Browser freeze with extension

    On Oct 23, 9:21*am, Nickolay Ponomarev wrote:
    > On Fri, Oct 23, 2009 at 10:47 AM, skuallpa wrote:
    > > During the execution of my javascript, I have somewhere a function
    > > that takes several seconds to return a value:

    >
    > > var result = myComponent.myFunction();
    > > if(result ==1)
    > > {
    > > *....
    > > }

    >
    > > During this time, the browser is freezing (for example, I can't change
    > > tab during this time). Is there a way to run this function in a way
    > > that the browser is not freezing?

    >
    > What does the function do?
    >
    > If you run a long operation on the main thread, the UI freezes, there's no
    > way around that.
    >
    > You can either (1) make it shorter (or run several short operations, chained
    > e.g. with setTimeout) or (2) run it on another thread.
    >
    > Usually you want option #1.
    >
    > The 'another thread' solution imposes certain restrictions on what you can
    > do there. You can't work with the DOM, in particular, so this is mainly
    > useful if you're doing calculations or working with something outside
    > mozilla (e.g. networking).
    > There are two possible ways to implement threading, one of which is using
    > DOM workers, introduced in recent versions (preferred, since it's harder to
    > mess up).
    >
    > Nickolay


    Thanks for your answer Nickolay,

    The function access webcam and I can't do it shorter. The function
    return a value (an int (PRInt16)).
    So I think I have no choice to use the "thread solution".

    This function do not work with DOM, it just make some calculations. I
    am reading some documentations "Using workers in extensions". If I
    understand well, I have to put the code that call the function of my
    component in a separate .js file. But my component is instanciate in
    the main .js with

    try
    {
    //create instance of component
    myComponent = Components.classes["@monsite.com/myComponent_cpp;1"]
    .getService(Components.interfaces.nsImyComponent);
    }
    catch (err)
    {
    alert(err);
    }

    Can I launch a method of this component in the separate .js file and
    return an integer value?

    Also, I want that the first script wait on the answer of this function
    (this thread) before continuing. How can I manage that?

    Thanks in advance for your help.


  3. Re: Browser freeze with extension

    >I don't know workers very well and I don't the specifics of what you're
    >doing in the function to be sure if workers or threads is a better solution
    >in your case (or if workers are usable from XPCOM components at all).


    I believed that workers and thread were similar (a worker do not
    launch a new thread?).

    The article "Using workers in extension" says that you have to use
    worker threads in extensions to perform tasks in the background
    without blocking the user interface. I think I have to use workers in
    my case, no?

    > That defeats the point of threading...

    I think a threading must be done because like that the interface is
    frozen the time the method return the result.

    I will study a little more what I can do with a callback function.


  4. Re: Browser freeze with extension

    Hello,

    I finally find the solution using nsIThreadManager (see article
    https://developer.mozilla.org/en/Code_snippets/Threads). I launch my
    function in a background thread and wait in the current thread until
    the backgroung thread return a value.

    var thread, currentThread;
    var complete = false;
    var result = 0;
    thread = Components.classes["@mozilla.org/thread-manager;1"]
    .getService(Components.interfaces.nsIThreadManager )
    .newThread(0);
    currentThread = Components.classes["@mozilla.org/thread-manager;1"]
    .getService(Components.interfaces.nsIThreadManager )
    .currentThread;
    var backgroundTask = {
    run: function() {
    result = myComponent.myFunction();
    complete = true;
    }
    }
    thread.dispatch(backgroundTask, thread.DISPATCH_NORMAL);

    while(!complete){
    currentThread.processNextEvent(true);
    }

    //do something with result
    ....

    By doing this way, the browser is no more frozen while calculating
    myComponent.myFunction();


    Thanks for your help.
    Regards

  5. Re: Browser freeze with extension

    > This is dangerous. The user could try to close the window or quit while
    > your loop is active. You are better off dispatching another event back
    > to the main thread that performs the next step.


    Thanks for the remark, but I don't see very well what you mean with
    "dispatching another event back to the main thread".
    What do I have to change? Can you give me more details?
    Thanks


  6. Re: Browser freeze with extension

    On Oct 28, 1:24*am, Neil wrote:
    > skuallpa wrote:
    > >>This is dangerous. The user could try to close the window or quit whileyour loop is active. You are better off dispatching another event back to the main thread that performs the next step.

    >
    > >Thanks for the remark, but I don't see very well what you mean with "dispatching another event back to the main thread".

    >
    > Something like this:
    >
    > var mgr = Components.classes["@mozilla.org/thread-manager;1"]
    > * * * * * * * * * * .getService(Components.interfaces..nsIThreadManage r);
    > var backgroundTask = {
    > * * * * callback: {
    > * * * * * * * * result: 0,
    > * * * * * * * * run: function() {
    > * * * * * * * * * * * * processResult(this.result);
    > * * * * * * * * }
    > * * * * },
    > * * * * run: function() {
    > * * * * * * * * this.callback.result = myComponent.myFunction();
    > * * * * * * * * mgr.mainThread.dispatch(this.callback, thread.DISPATCH_NORMAL);
    > * * * * }}
    >
    > var thread = mgr.newThread(0);
    > thread.dispatch(backgroundTask, thread.DISPATCH_NORMAL);
    >
    > --
    > Warning: May contain traces of nuts.


    Thanks for your help Neil,

    I have modified my code and all works well now.

    Regards

+ Reply to Thread