stopping threads in CF

  • Thread starter Thread starter Andreas Möller
  • Start date Start date
A

Andreas Möller

Hello NG,

it's no problem to start threads in compact framework. But how can i stopp
them? There is no method called stop.

greetings
Andreas Möller
 
What you need to do is create the logic yourself to terminate your thread
properly.

Just use a flag that you set when you need to terminate your thread.

For instance

// great location to add your worker thread initialisation code

while (! bReady)
{
// do whatever you need to do in your thread
}

// great location to add your worker thread termination code


Outside your thread set bReady true when you need to terminate. If you are
waiting for events (synchronisation objects etc.) in your thread make sure
to 'fire' an event as well when you want to abort, otherwise your thread
will not terminate until the particular event is set.
 
There is no Thread.Abort in the CF, so you must use something like Maarten's
suggested global flag.
 
What do I write in ** // great location to add your worker thread
termination code **

Basically, I want to start a thread and let's say open my database
connection then set it to some variable and now I don't want the overhead of
the thread that I started. What exactly happens to that thread.
 
The worker thread needs to continually check a global boolean flag. When
the prmary thread wants to kill the thread, it sets the flag. The thread
sees that change and the code in the thread then exits. You have to write
all of these steps as none is automagic.
 
Venue,

If the thread calls return, then it stops running. The only way to tell it
to call return is by having some variable that both threads can see that
indicates that the thread should kill itself by returning.
 
I followed the sample and have my one class calling two threads (from two
methods) and another thread from another class.

When I run my code in debug mode I see that this.CloseMe() gets called only
once although I have the line < this.Invoke (new
EventHandler(this.CloseMe)); > in all my methods calling threads. I tried
creating two different methods for closing i.e. CloseMe1 and CloseMe2 but
only one of them gets called ( I guess the thread that completes first).
Does that mean other thread gets aborted (even in the other class where I
start the thread doesn't get executed).

Does that mean that this.CloseMe should be called only when the application
is about to shut down. But then how do I get rid of the thread which
finished the job it was assigned to do.

Also, will it be sufficient to just return from the method which is being
called by thread when certain condition is true.
 
Back
Top