(keitai-l) Re: Moving work to the docomo event Thread

From: Trent Hill <Trent.Hill_at_bullant.com.au>
Date: 06/13/03
Message-ID: <8AFFE88038D085458C8B0E604D9B3D460155C9CD@sydmail1.au.bullant.ads>
Hi Jason,

> What exactly are you trying to do?

It's quite simple really - my application has created a thread. Some time 
later, this thread needs to update the user interface eg. to change the text

on a label. Now, as far as I know the nttdocomo.ui library is not thread
safe 
ie. it is only safe to call Label.setText from the event handling thread. So

I somehow need to execute the necessary code on the ui thread.

If I were using C/Win32 I'd use PostMessage(...)
If I were using Java/Swing I'd use SwingUtilities.invokeLater(...)
If I were using Java/Midp I'd use Display.callSerially(...)

I can find anything like this in the com.nttdocomo.ui library.

> Can you give a pseudo-code example?

Of course.. this is not my real application but here's a similar example:

Imagine you had a ui with a button and a label. When the user clicks the
button, a stock quote is downloaded from an HTTP server and displayed on
the label.

Now, the HTTP download is blocking and I do not want to block the main event

handling thread while the download is occurring eg. I may want a cancel 
button to be available during the download attempt. Therefore, I must create

a second thread to handle the HTTP download. When the download succeeds, I 
need to update the label from the event handling thread to be thread safe.

So, in pseudo code:

  // Event handling thread:
  method handleEvent(event, arg)
  {
    if (event == buttonClick)
    {
      // create new thread to download quote
      new Thread(downloadQuote)
    }
    else if (event == dataReceived)
    {
      // display the new quote
      myLabel.setText(arg)
    }
  }


  // Network thread:
  method downloadQuote
  {
    openHttpConnection(myUrl)
    quote = downloadData() // <- long blocking operation
    closeHttpConnection()

    // somehow notify event handling thread since I cannot safely update
    // the ui from this thread
    sendMessage(handleEvent, dataReceived, quote) // *** <- how to do this??
  }

I hope this makes some sense to you. Any assistance would be appreciated.

Thanks, Trent.

--
Trent Hill
Software Engineer
Gravana Pty Ltd (trading as Bullant Software)
Received on Fri Jun 13 07:31:34 2003