Interthread Communication

Cited from mozillaZine Forum.

Posted by tores.

If you want to call a method which performs graphical works but you are

not in the graphical thread, you have to create a proxy from your

object and call the method on this proxy instead of your object.

////////  See for C++ Implementation  /////////

Set to
STATE_UNJOINABLE if you wish to have the main thread return control

after starting the worker thread.

////////   Add Observer //////////
function InterfaceAPI()
InterfaceAPI.prototype = {
    observerService : null,
    register: function () {
        /* Register “UI” observer callback */
        this.observerService = Components.classes[“;1”]
    /* Callback for observed data on topic “UI” */
    observe: function(subject ,topic, data){
        alert(“Response: ” + data);

/////////  Notify Observer ///////////
/* 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[“;1”].createInstance(nsIThread);
        thread.init(this,                       /* Execute run() in this object */

    /* Separate thread */
    run: function ()
        /* Get UI thread event queue */
        const nsIEventQueueService = Components.interfaces.nsIEventQueueService;
        var eqService = Components.classes[“;1”]
        var uiQ = eqService.getSpecialEventQueue(nsIEventQueueService.UI_THREAD_EVENT_QUEUE);
        /* Create basic observer */
        var observerService = Components.classes[“;1”]

        /* Create a proxy around the observer */
        var proxyObjectManager = Components.classes[“;1”]
        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,
        /* Notify other thread */
        this.proxyObserverService.notifyObservers(null,”UI”,”I’m alive!”);

new InterfaceAPI();

technorati tags:, , , ,

Blogged with Flock

3 Responses to “Interthread Communication”

  1. Lululemon Sale Says:

    I constantly emailed this website post page to all my friends, because if like to read it after that my contacts will too.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: