Tag: thread

Process incoming data (queue overflow)

When you develop applications for the BlackBerry smartphone, any network communication or lengthy data processing should be performed in its own thread. This is easy to realize for network input/output (I/O) because network connections must be initiated in their own thread, otherwise a deadlock situation may occur.[1]

When an application performs lengthy processing on the main event thread, the BlackBerry smartphone user can perceive the application to be slow because it is unable to process input commands. In an extreme situation, the application can close if it blocks or ties up the main event thread. If an application ties up the main event thread, the system is unable to dispatch messages to it (for example, BlackBerry smartphone user input, UI updates or network I/O). If the application message queue exceeds its limit, the application is terminated with the one of the following errors[2]:

Process [application name] killed due to message queue overflow

or

Uncaught Exception: [ApplicationName] is not responding; process terminated

Cause
An application is blocking the main event thread.

The main event thread processes system messages. Blocking this thread prevents system messages from being processed and can lead to a message queue overflow.

Because there is a finite amount of storage space for messages, the issue is caused when these messages are not being processed. If an application stops processing messages for a long period of time, the system assumes that the application has stopped responding and is no longer functioning as expected. This is the reason why the process was terminated.

Resolution
Move long processes or processes that block to a separate background thread.

Application stops responding when opening a connection[3]

Flow of what is occurring

  • Application opens connection
  • UI pushing dialog box to the foreground
  • BlackBerry is now locked

Flow of what should occur

  • Application places a connection into a thread
  • Application runs the connection in a thread off of the main event thread
  • UI pushes dialog box to the foreground and thread execution is currently paused
  • User is notified of connection and accepts
  • System returns execution to the connection thread

[1] http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/How_To_-_Process_incoming_data.html?nodeid=1162112&vernum=0
[2] http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800783/Support_-_Process_[ApplicationName]_killed_due_to_message_queue_overflow.html?nodeid=826932&vernum=0
[3] http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/796557/800451/800563/Support_-_My_Application_Hangs_When_Opening_a_Connection.html?nodeid=800455


Update GUI from background listener

detect gui,
getSingleton,
update status from background thread [1]

/**
 * FolderListenerGUI.java
 *
 * Author: Mark Sohm
 */

package com.samples.folderListener;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;

public class FolderListenerGUI extends UiApplication
{
  private MainScreen mainScreen;
  private static RichTextField newMailNotifier;

  public static void main(String[] args)
  {
    boolean startup = false;

    //Check parameters to see if the application was entered
    //through the alternate application entry point.
    for (int i=0; i<args.length; ++i)
    {
      if (args[i].startsWith("init"))
      {
        startup = true;
      }
    }

    if (startup)
    {
      //Entered through the alternate application entry point.
      //Enable folderlistener on startup.
      FolderListenerApp.waitForSingleton();
    } else
    {
      //Entered by selecting the application icon on the ribbon.
      //Start the FolderListenerGUI.
      FolderListenerGUI app = new FolderListenerGUI();
      app.enterEventDispatcher();
    }
  }

  public FolderListenerGUI()
  {
    //Pass a reference to the FolderListenerGUI
    //to the FolderListenerApp.
    FolderListenerApp.waitForSingleton().
      setFolderListenerGUI(this);

    //Initialize mainScreen.
    mainScreen = new MainScreen();

    //Initialize the fields.
    newMailNotifier = new RichTextField("No mail yet...",
      RichTextField.NON_FOCUSABLE);
    LabelField title = new LabelField("Folder Listener GUI",
      LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
    mainScreen.setTitle(title);
    mainScreen.add(newMailNotifier);

    //Display the main screen
    pushScreen(mainScreen);
  }

  //Update the RichTextField with a new mail message.
  public static void NewMailArrived()
  {
    newMailNotifier.setText("New mail arrived!");
  }

  //Remove the FolderListenerGUI instance from
  //FolderListenerApp on exit.
  protected void onExit()
  {
    //Remove the reference to FolderListenerGUI
    //from FolderListenerApp.
    FolderListenerApp.waitForSingleton().unsetFolderListenerGUI();
  }
}

/**
 * FolderListenerApp.java
 *
 */

package com.samples.folderListener;

import net.rim.blackberry.api.mail.*;
import net.rim.blackberry.api.mail.event.*;
import net.rim.device.api.system.*;
import net.rim.blackberry.api.mail.*;

public final class FolderListenerApp implements FolderListener {

  private FolderListenerGUI folderListenerGUI = null;
  //Long value: com.samples.folderListener
  public static final long RTSID_MY_APP = 0x7451402f595f81a5L;

  public FolderListenerApp()
  {
    try
    {
      //Get the store from the default instance.
      Store store = Session.getDefaultInstance().getStore();
      //Add the folder listener to the store.
      store.addFolderListener(this);
    }
    catch (Exception e)
    {
      System.out.println(e.toString());
    }
  }

  //Returns an instance of the running FolderListenerApp.
  public static FolderListenerApp waitForSingleton()
  {
    //Ensure this is a singleton instance.

    //Open the RuntimeStore.
    RuntimeStore store = RuntimeStore.getRuntimeStore();
    //Obtain the reference of FolderListenerApp.
    Object obj = store.get(RTSID_MY_APP);

    //If obj is null, there is no current reference
    //to FolderListenerApp. Start a new instance
    // of FolderListenerApp if one is not running.
    if (obj == null)
    {
      //Store a reference to this instance in the RuntimeStore.
      store.put(RTSID_MY_APP, new FolderListenerApp());

      return (FolderListenerApp)store.get(RTSID_MY_APP);
    } else
    {
      return (FolderListenerApp)obj;
    }
  }

  //Called when a new message is created.
  public void messagesAdded(FolderEvent e)
  {
    //Get the message that fired the folder event.
    Message orginalMessage = e.getMessage();
    //Get the folder the message is in.
    Folder messageFolder = orginalMessage.getFolder();

    //Is the new message in the Inbox?
    if (messageFolder.getType() == Folder.INBOX)
    {

      //Check if the FolderListenerGUI is running.
      if (folderListenerGUI != null)
      {

        //Grab the lock for the running FolderListenerGUI.
        folderListenerGUI.invokeAndWait(new Runnable()
        {
          public void run()
          {
            //Call the NewMailArrived method to update the
            //screen with a notification.
            folderListenerGUI.NewMailArrived();
          }
        });
      }
    }
  }

  //Passes in reference to the FolderListenerGUI that is running.
  public void setFolderListenerGUI(FolderListenerGUI folderGUI)
  {
    folderListenerGUI = folderGUI;
  }

  //Set the reference to the FolderListenerGUI to null
  //(when it is shut down).
  public void unsetFolderListenerGUI()
  {
    folderListenerGUI = null;
  }

  //Not used in this sample.
  public void messagesRemoved(FolderEvent e)
  {
  }
}

[1] http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800783/How_To_-_Allow_a_background_listener_to_detect_and_update_a_GUI_application.html?nodeid=827026&vernum=0


  • Copyright © 1996-2010 vaks.in. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress