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
Trusted Contributor
Posts: 138
Registered: ‎04-19-2012
My Device: No
My Carrier: Nokia
Accepted Solution

Start n stop the Thread

Hello all.

 

i need to develope a program of thread.

i need two buttons 

1- start

2 stop

 

on these two buttons i need to perform some task.

 

when i press start..the somethings start happening.

when i press stop all the thread stop.

 

and againg i press start ..i should start

 

 

help me.

 

 

Himanshu

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Start n stop the Thread

 

Hi @

 

A simple (and very naive) way to do it is:

 

1. On a clicking the start button, you check if the thread is not null and you start it.

 

2. In the thread, hold a while loop with a flag which was set to true (on step #1).

 

2. When clicking on the stop button, set the flag value to false so the loop would stop and the thread would finish its work.

 

Hope that helps,

 

E.

Trusted Contributor
Posts: 138
Registered: ‎04-19-2012
My Device: No
My Carrier: Nokia

Re: Start n stop the Thread

Thanks for reply. 

 

Like  this or some else

 


public void threadStart()
{
Thread thread = new Thread()
{
boolean active = true;
public void run()
{
while(active)
{
int i =1;
i++;
}


}

};
thread.start();
}

Trusted Contributor
Posts: 138
Registered: ‎04-19-2012
My Device: No
My Carrier: Nokia

Re: Start n stop the Thread

Give me any good code ..

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Start n stop the Thread

Basically, yes.

 

But I recommend to create a separate class which extends Thread.

 

This way you will have less problems with changing active flag value.

 

E.

Trusted Contributor
Posts: 138
Registered: ‎04-19-2012
My Device: No
My Carrier: Nokia

Re: Start n stop the Thread

exactly my problem is i want to show "Hello"on my device with press start button.

until i dont press stop . it hello should displayed regular  after every second like this.

 

hello

hello

hello

hello

.

.

.

.hello

 

but after press stop it should stop...but

 

if i press start again it should start

Trusted Contributor
Posts: 138
Registered: ‎04-19-2012
My Device: No
My Carrier: Nokia

Re: Start n stop the Thread

plz give me any solution.

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Start n stop the Thread

Here are couple of options:

 

1. Let your thread run consistency and only do your work if your active flag set to true.

 

2. After the thread is stop, re-initiate it:

 

myThread = new MyThread();

myThread.start();

Trusted Contributor
Posts: 138
Registered: ‎04-19-2012
My Device: No
My Carrier: Nokia

Re: Start n stop the Thread

not done ..i need help now.

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

Re: Start n stop the Thread

use a http://www.blackberry.com/developers/docs/7.1.0api/java/util/Timer.html
schedule a timertask when you start, and cancel it when you stop.
----------------------------------------------------------
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