1
votes

I'm developing a Windows Mobile 5.0 and above application using .Net Compact Framework 2.0 SP2 and C#.

How can I know when a thread ends?

This is my code:

System.Threading.Thread thread1 =
    new System.Threading.Thread(() => RetreiveSoMuchData(ID));
thread1.Start();

Thank you.

2

2 Answers

3
votes

You'll need to get the thread to:

  • Fire an event that someone is listening to
  • Set a value someone else is checking
  • Set a wait event that another thread is waiting on (e.g. a AutoResetEvent or ManualResetEvent)
  • Return on an infinite join() command (on that thread)

Unfortunately neither of these leaves the thread alone or doesn't pull in other threads, but this is the joy of multithreading. I'd generally go for firing an event myself. The BackgroundWorker object in the Full Framework does this but unfortunately is not available in CF so you'll have to write that yourself (perhaps write a wrapper for the Thread class that does this for you).

1
votes

Since you're spawning your own thread, as soon as RetrieveSoMuchData(..) finishes, the thread will terminate.

You can notify yourself of this by using an EventWaitHandle, such as a AutoResetEvent.