Paul said:
Well, the thread, when it's not doing its work, might instead wait, using
WaitForSingleObject() (which is wrapped in OpenNETCF, too), on a global
event. When the main thread wants the thread to exit, it would set the
event, causing the thread to return from WaitForSingleObject() and, upon
checking the return value, realize that it's time to exit. It would then
simple return. If the thread is waiting for data from a socket as part of
its work, the main thread might set the event and close the socket, assuring
that, even if the socket is blocked, it will return control to the thread
and the thread can see that it's time to leave. If the thread is waiting
for serial data from a serial port, the main thead might set the event and
close the port handle. If the thread is performing some mathematical
operations, it could periodically check the status of the event and exit if
the event was set.
You assume this: "When it's not doing its work", this is the problem!!
My thread always work.
this is the code:
int now = DateTime.Now.Second + DateTime.Now.Minute * 60;
int timeLimit = now + 60;
do
{
//perform a connection with start tracking
App.GpsReceiver.StartTracking();
now =DateTime.Now.Second + DateTime.Now.Minute * 60;
if ( App.GpsReceiver.IsTracking() )
{
//the connection goes ok!
this.PostGpsConnection(true);
return;
}
}
while ( timeLimit > now && !App.GpsReceiver.IsTracking() );
the problem is App.GpsReceiver.StartTracking();
this fuction doesn't return immediatly but it can be 4-5-6-7 or even 20 seconds long.
(it perform a special handshake between the pocketpc and the gps)
So as you may understant, if I insert a flag like this:
while ( !threadStop && timeLimit > now && !App.GpsReceiver.IsTracking() );
or like this
WaitForSingleObject(Whatever,1000)
the exit from thread can get after very long time for the moment of required stop.
In your 3 second case, what's the big deal? The goal is simply to get the
thread to exit, not to have it exit within x milliseconds, right? You can
certainly wait in the main thread for the thread to exit
(WaitForSingleObject( threadHandle )), if you want to, but you're not
*required* to.
If you have the crash while running your program in the VS.NET debugger,
what happens? VS.NET exits? The program exits silently? An exception is
thrown? As far as I know, none of us is a mind reader.
The program fire an exception, now I haven't to program in my hand, so tomorrow I will give you
more details