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
avinash_kumar_ispg_in
Posts: 172
Registered: ‎01-02-2012
My Device: 8520 curve
My Carrier: compny

How to stop running background process using cancel key

Hi every one

 

    I have created on application in that I am running background thread in which I am gathering the information from the server and till that time I have putted the indicator on the screen .

       I wanted to cancel that background process using cancel button how it is possible please reply me any one

thanks in advance

Please use plain text.
Developer
alishaik786
Posts: 285
Registered: ‎08-26-2011
My Device: 9900
My Carrier: Not Specified

Re: How to stop running background process using cancel key

we cannot kill the thread; But we can stop the thread indirectly;

 

Try this sample:

 

public class ThreadClass extends MainScreen
{
	private boolean isKeepGoing=false;
	public ThreadClass() 
	{
		createGUI();
	}	

	private void createGUI() 
	{
		ButtonField start=new ButtonField("Start", Field.FIELD_HCENTER);
		start.setChangeListener(new FieldChangeListener() 
		{
			public void fieldChanged(Field field, int context) 
			{
				isKeepGoing=true;
				startThread();
			}			
		});
		add(start);
		
		ButtonField stop=new ButtonField("Stop", Field.FIELD_HCENTER);
		stop.setChangeListener(new FieldChangeListener() 
		{
			public void fieldChanged(Field field, int context) 
			{
				isKeepGoing=false;
			}			
		});
		add(stop);
	}
	
	private void startThread() 
	{
		Thread thread=new Thread()
		{
			public void run() 
			{
				while(isKeepGoing)
				{
					//You have to write all the code between this loop only;
					try 
					{
						Thread.sleep(100);
						System.out.println("========"+Thread.activeCount());
					} 
					catch (Exception e) 
					{
						
					}
				}
			}
		};
		thread.start();
	}
}

 See the start and stop in the fieldChangeListener;

=================================================================================
Feel free to click LIKE button if the solution helps you;
--
Regards,

ALI SHAIK.
Please use plain text.
Developer
avinash_kumar_ispg_in
Posts: 172
Registered: ‎01-02-2012
My Device: 8520 curve
My Carrier: compny

Re: How to stop running background process using cancel key

Hi 

   while the indicator is showing at that time the back button not working ,so can you tell me which method invoked while back ground process is running.

thanks in advance

Please use plain text.
Developer
simon_hain
Posts: 16,102
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to stop running background process using cancel key

i have no clue what you are asking, could you try to describe your problem with a tad more detail?
(and a single thread is sufficient for one problem)
----------------------------------------------------------
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
Please use plain text.
Developer
avinash_kumar_ispg_in
Posts: 172
Registered: ‎01-02-2012
My Device: 8520 curve
My Carrier: compny

Re: How to stop running background process using cancel key

Hi

  while my thread is running at that time the onclose() and keyDown() method not invoking,So where should i keep the code to cancel the background thread

thanks

Please use plain text.
Developer
simon_hain
Posts: 16,102
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to stop running background process using cancel key

do you start your thread with start()?
maybe you can post some meaningful code snippets.
----------------------------------------------------------
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
Please use plain text.
Developer
avinash_kumar_ispg_in
Posts: 172
Registered: ‎01-02-2012
My Device: 8520 curve
My Carrier: compny

Re: How to stop running background process using cancel key

Hi this is my code

 

//This is to run the background thread and to show the indicator

 

 

threadToRun = new Thread()
{
public void run()
{
// First, display this screen
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{ // to display the indicator
UiApplication.getUiApplication().pushScreen(thisScreen);
}
});


//Here is my back ground process code



UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
// to pop the indicator screen
UiApplication.getUiApplication().popScreen(thisScreen);

}
});
}
};
threadToRun.start();

 

 

 

 

 

//This is the method to stop the bacground thread


public void stop() {
if ( threadToRun != null ) {

if ( threadToRun.isAlive() ) {
threadToRun.interrupt();

// This will send us a 'failure' notification
}
} else {
// Something has gone really wrong?!
throw new RuntimeException("Oppsss");
}
}

 

 

 

     i want to call my stop method while pressing the back button .So how to call it please reply 

thanks in advance

Please use plain text.
Developer
simon_hain
Posts: 16,102
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to stop running background process using cancel key

go back and read alishaik786 post.
interrupt is not a good way to end the processing.
----------------------------------------------------------
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
Please use plain text.
Developer
avinash_kumar_ispg_in
Posts: 172
Registered: ‎01-02-2012
My Device: 8520 curve
My Carrier: compny

Re: How to stop running background process using cancel key

[ Edited ]

Hi

But I want to cancel background process in first loop only,how it is possible please reply.

 

 (actually in background I am putting the downloading process , in case if network is slow the downloading will be slow so in this case I want to cancel the background process(to cancel downloading) ) is it possible using loop.

If not reply me the other way.

Thanks

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to stop running background process using cancel key

The code sample given previously is very similar to code that I use to cancel a background Thread that is downloading data over the internet.  So that should do the job.

 

The issue perhaps is that it not possible using this sort of code, to break in a http connection that is waiting for  response - so it will continue and you may need to check stop flag a number of times in the loop processing, for example after the connection has completed.

 

One option to try to do stop this more immediately is to close the connection.  So your stop method in your Thread also tries to close the http connection.  If this is successful, then the connection will give an error, so you need to make sure that the user does not get notified of this (it is not really an error).  My experience of trying to do this is mixed shall we say, I would not guarantee that it will work. 

Please use plain text.