...or take care of threading properly - instead of Thread.sleep, use a
monitor and wait on it with a timeout. Here's the pseudo code
(catching InterruptedException omitted):

// in your main thread

// when you launch the secondary thread make sure it's running before
you do anything else
synchronized (secondaryThreadRunning)
{
    secondaryThread = new ...
    secondaryThread.start();
    secondaryThreadRunning.wait();
}

// before you exit the app (e.g. onStop)
synchronized (secondaryThreadStopMonitor)
{
    secondaryThreadShouldStop = true;
    secondaryThreadStopMonitor.notify();
    secondaryThread.join(); // wait for it to finish
}

// 1st line in the secondary thread's run()
// signal main thread we're up and running
synchronized (secondaryThreadRunning)
{
    secondaryThreadRunning.notify();
}

// in the secondary thread where you want to wait and then post
synchronized (secondaryThreadStopMonitor)
{
    secondaryThreadStopMonitor.wait(your timeout);
    if (secondaryThreadShouldStop)
    {
        // break the loop (if you have one) and exit the thread (i.e.
let it return from run())
    }
    else
    {
       // timed out, post to your handler [1]
    }
}

[1] You might get a spurious wake here, so it's best to track the
elapsed time manually

In any case, make sure you're not doing too much work, it seems that
you can simply use:

Handler.postDelayed(Runnable r, long delayMillis) and in your handler
do it again


Cheers,
Stoyan

On Wed, Feb 18, 2009 at 7:48 AM, Romain Guy <romain...@google.com> wrote:
>
> In your application's onPause/onStop/onDestroy simply use
> Handle.removeCallbacks(Runnable).
>
> On Tue, Feb 17, 2009 at 9:46 PM, Dilli <dilliraomca...@gmail.com> wrote:
>>
>> Hi all,
>>
>>
>> I am developing a simple application with thread concept
>>
>> problem:
>>
>>  In one thread i write
>>
>>         {
>>           Thread.sleep(10000)  //10 sec sleep
>>           m_handler.post(my_runnable);  // to post to run another
>> runnable
>>         }
>>
>>
>>  before  the thread wake up ( <10 sec)  i stopped my application
>> exiting my application
>>
>>  but the thread sleeped not killed and it wakes up after 10 sec and
>> try to post message
>>  and it causes exception.
>>
>>
>>  is there any way to kill the threads those are in sleep mode while
>> exiting the application ??
>>
>>  and prevent to post using m_handler.
>>
>>  Need suggetions
>>
>> Thank You..
>>
>> Dilli.
>>
>>
>>
>>
>>
>>
>> >
>>
>
>
>
> --
> Romain Guy
> Android framework engineer
> romain...@android.com
>
> Note: please don't send private questions to me, as I don't have time
> to provide private support.  All such questions should be posted on
> public forums, where I and others can see and answer them
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to