MozillaZine

Interthread communication [SOLVED]

Discuss building things with or for the Mozilla Platform.
tores
 
Posts: 8
Joined: January 17th, 2005, 11:14 am

Post Posted June 6th, 2006, 10:55 am

Hi,

After doing some research I found a way to create an extension consisting of several threads.
I've been able to create multiple threads, but have encountered problems when setting up the inter-thread communication.

I'd really like to use oberservers for this since they are visible from every window in Firefox.
The problem is that using basic observers as communication channels chrashes Firefox.

So I've read that wrapping objects inside proxies might solve the problem:
http://www.mozilla.org/projects/xpcom/Proxies.html

The problem is that I can't seem to get my proxy to work. I've followed examples where their used:
http://www.koders.com/javascript/fidCDF ... 4956C.aspx
And doing it the same way doesn't work for me.

When running the code below, Firefox locks up as proxyObjectManager.getProxyForObject gets called.

I'm not sure that this is the correct way to set up communication between threads.
But, if anyone has any knowledge as to how this should be done, I would appreciate to take part in that knowledge:)

Here is my code:
Code: Select all
function InterfaceAPI()
{
    this.register();
}
InterfaceAPI.prototype = {
 
    observerService : null,
   
    register: function () {
        /* Register "UI" observer callback */
        this.observerService = Components.classes["@mozilla.org/observer-service;1"]
                              .getService(Components.interfaces.nsIObserverService);
        this.observerService.addObserver(this,"UI",false);
    },
   
    /* Callback for observed data on topic "UI" */
    observe: function(subject ,topic, data){
        alert("Response: " + data);
    }
   
};


/* Client watcher running in separate thread */
var Watcher = {
    proxyObserverService: null,
   
    /* Call this to start the thread */
    threadStart: function()
    {
        const nsIThread = Components.interfaces.nsIThread;
        var thread = Components.classes["@mozilla.org/thread;1"].createInstance(nsIThread);
        thread.init(this,                       /* Execute run() in this object */
                    0,
                    nsIThread.PRIORITY_NORMAL,
                    nsIThread.SCOPE_GLOBAL,
                    nsIThread.STATE_JOINABLE);
    },

    /* Separate thread */
    run: function ()
    {
        /* Get UI thread event queue */
        const nsIEventQueueService = Components.interfaces.nsIEventQueueService;
        var eqService = Components.classes["@mozilla.org/event-queue-service;1"]
                              .getService(nsIEventQueueService);
        var uiQ = eqService.getSpecialEventQueue(nsIEventQueueService.UI_THREAD_EVENT_QUEUE);
       
        /* Create basic observer */
        var observerService = Components.classes["@mozilla.org/observer-service;1"]
                              .getService(Components.interfaces.nsIObserverService);

        /* Create a proxy around the observer */
        var proxyObjectManager = Components.classes["@mozilla.org/xpcomproxy;1"]
                                 .getService(Components.interfaces.nsIProxyObjectManager);
        var proxyType = proxyObjectManager.INVOKE_ASYNC | proxyObjectManager.FORCE_PROXY_CREATION

        /* !!!!!!! Firefox chrashes during getProxyForObject call !!!!!!!
         * EDIT: The second parameter was not syntactically correct. Fixed now, and the code works. */
        this.proxyObserverService = proxyObjectManager.getProxyForObject(uiQ,
                                                                         Components.interfaces.nsIObserverService,
                                                                         observerService,
                                                                         proxyType);
        /* Notify other thread */
        this.proxyObserverService.notifyObservers(null,"UI","I'm alive!");
    }
   
};

new InterfaceAPI();
Watcher.threadStart();

FrederikVds
 
Posts: 18
Joined: May 4th, 2006, 11:57 am

Post Posted July 13th, 2006, 12:17 pm

How did you solve it? I'm having the same problem.

Oh I see, the edit in your code is the fix?

Thank you so much! It works!

I wouldn't know what to do without this post!

rvikesh
 
Posts: 9
Joined: March 25th, 2006, 11:57 am

Post Posted August 11th, 2006, 5:59 am

hi,
i need to create an extension with multiple threads but i am having problem in implementing it. i try to look for it on the web but was not successful. can u plz help me..can u give me an example with source code

FrederikVds
 
Posts: 18
Joined: May 4th, 2006, 11:57 am

Post Posted August 14th, 2006, 1:44 pm

The first post contains source code.

ext
 
Posts: 39
Joined: April 19th, 2006, 7:00 am

Post Posted June 27th, 2007, 7:52 am

Does anybody know how this could run on trunk builds(1.9er)?
Beacuase there is now @mozilla.org/thread;1 on trunk.

Code: Select all
threadStart: function()
{
        const nsIThread = Components.interfaces.nsIThread;
        var thread = Components.classes["@mozilla.org/thread;1"].createInstance(nsIThread);
        thread.init(this,                       /* Execute run() in this object */
                    0,
                    nsIThread.PRIORITY_NORMAL,
                    nsIThread.SCOPE_GLOBAL,
                    nsIThread.STATE_JOINABLE);
}


I only want to stop a script that is running for 30-90seconds sometimes, so that the Browser remains responsible for User interactions.

Return to Mozilla Development


Who is online

Users browsing this forum: No registered users and 0 guests