BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 02-13-2007, 02:41 PM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: Sep 2006
Location: Ontario, Canada
Model: 9700
Carrier: Rogers
Posts: 92
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default I have been playing with Treads, and I am having some trouble...

Please Login to Remove!

Hey all, here's what I'm doing

I have my UI, which starts 2 threads.

The first thread simply constantly incrememnts a number within the run command

Something like this:
Code:
    public void run()
    {
        numTimes = 0;
        quit = false;
        while (!quit)
        {
            if (numTimes == 1234567)
            {
                quit = true;
            }
            numTimes++;
            try
            {
                sleep(500);
            }
            catch(InterruptedException e) {}
        }
     }
My other thread is launched on a timer, and simply grabs that number. That's all I want to do.

So here is the timer

Code:
       
        Timer timer = new Timer();
        myTask thetask = new myTask(_gdt);
        timer.schedule(thetask, 5000, 10000);
(_gdt) is the threaded object with the constantly updating integer value.

here is the run command in the myTask object

Code:
        public void run()
        {
            UiApplication.getUiApplication().invokeAndWait(new Runnable()
            {
                public void run()
                {
                    Dialog.alert(Integer.toString(_gdt.getTimes()));
                }
            });
        }
Now this all compiles just fine, but it hangs the system when I try to launch the app.

Any ideas?

Thanks all!
__________________
http://www.ryannickel.com for my awesomeness!
Offline  
Old 02-13-2007, 03:23 PM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Sep 2006
Location: Ontario, Canada
Model: 9700
Carrier: Rogers
Posts: 92
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think I've figured it out. I was calling Thread.run(), instead of Thread.start();

Gotta know that as soon as I ask for help I figure it out myself. SHEESH! haha

If anyone has any pointers on using threads, or any advice on working with them, I'm open to suggestion!

Thanks again all!
__________________
http://www.ryannickel.com for my awesomeness!
Offline  
Old 02-14-2007, 08:35 PM   #3 (permalink)
Knows Where the Search Button Is
 
Join Date: Nov 2006
Model: 7250
Carrier: Carrier Has Arrived
Posts: 29
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

if at other times you need a thread to run in background
and just call make the thread do a task at any given time,
you can try something like this

Code:
.........// declarations or whatever here....

public void run()
{
  while (true)
  {
    if (killThisThread)
    {
      return;
    }
    else if (!killThread && !executeTask)
    {
      try
      {
        sleep(100);
      }
      catch (InterruptedException ie)
      {}
    }
    else if (!killThread && executeTask)
    {
      doTheTask();

      // reset so thread waits until you 
      // need it to execute task once again
      executeTask = false; 
    }
  }
}

public void killThread()
{
  killThisThread = true;
}

public void timeToDoTask()
{
  executeTask = true;
}

Last edited by newToThis : 02-14-2007 at 11:22 PM.
Offline  
Old 02-21-2007, 10:41 AM   #4 (permalink)
Thumbs Must Hurt
 
Join Date: Sep 2006
Location: Ontario, Canada
Model: 9700
Carrier: Rogers
Posts: 92
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks!

Thanks, I'll def. use that!
__________________
http://www.ryannickel.com for my awesomeness!
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.