Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 58
Registered: ‎06-06-2009
My Device: Not Specified
Accepted Solution

Stop Thread

Hi all,

I have a strange problem with the thread

i do some tasks in thread and i want to stop the thread on close but i could not do that

 

I use myThread.interrupt() but it not do anything the thread still working even when the screen closed.

 

the following is sample of what i do

 

 workingThread = new Thread(new Runnable()
        {
            public void run()
            {
                try
                {

                    //getdatafromWeb();

 

                   for(int i=0;i<count;i++)
                   {
                      final byte[] imageData=getImage(currentUrl);
                      UiApplication.getUiApplication().invokeAndWait(new Runnable()
                      {
                        public void run()
                        { 
                          if(imageData!=null)
                           {
                               currentScreen.add(new BitmapField(....));

                           }
                        }           

                     });
                 }

                }

                catch(Exception e)
                 {
                 }
                     
            }
        });
  workingThread.start();

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Stop Thread

you can put a boolean into your for count that interrupts if set to false.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Stop Thread

The only good way to exit a thread is to return from the run() method. Interrupting a thread, particularly one that is doing I/O, is a recipe for an unstable system. (The documentation says that interrupt() "is not required to cancel or clean up any pending I/O operations that the thread may be waiting for"; it isn't kidding.)

 

Create your thread class so it has access to an "exit requested" variable that you can set from another thread. (Best to declare the variable as volatile.) Within your working thread, check the variable once in a while (e.g., in the for loop as Simon suggests) and return if it is set.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 58
Registered: ‎06-06-2009
My Device: Not Specified

Re: Stop Thread

thanks all

 

I used the boolean variable and it stopped the thread (but i hade to check it in every step (or function) in the thread)

Developer
Posts: 104
Registered: ‎08-03-2010
My Device: Blackberry Z10
My Carrier: Idea

Re: Stop Thread

[ Edited ]

@simon_hain ..

I coudn't understand ..

Can u give desc about it??


Please mark posts as solved if you found a solution.
Feel free to press the kudos button to thank the user that helped you
New Contributor
Posts: 3
Registered: ‎04-11-2011
My Device: 8900
My Carrier: Voda

Re: Stop Thread

try this

boolean cancel = false;

while( the loop && cancel == false) {
do something
}

i create a button to make cancel boolean to true
Regular Contributor
Posts: 59
Registered: ‎03-01-2011
My Device: Not Specified

Re: Stop Thread

i was searching for it. And ur suggestion solved my issue too. Smiley Happy thankz simon

Highlighted
Developer
Posts: 455
Registered: ‎01-14-2009
My Device: Apple iPhone 3GS 16GB

Re: Stop Thread

just stumbled upon this link which is relevant to the discussion here .http://www.javamex.com/tutorials/threads/stopping_thread.shtml

 

Very good read about threads ,simple and informative.