Aborting a Thread

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I'm writing an application in C# for PocketPC 2003. The application should use a thread to read data from a bluetooth connection. If the connection is no longer needed I want to stop and destroy the thread. As I understand the .NET CF does not support Thread.Abort(). How can threads be terminated in .NET CF

Thanks for your help
Kind regards

Dominik
 
Hi Dominik,

There is indeed no functionality in .NET CF to abort a thread. So far
I've encountered two solutions to get around this:

1. use a flag to request the thread to stop running.
2. set the thread object to nil (Nothing in VB).

I prefer method 1. In pseudo-code it would look something like this:

class MyClass {

stopFlag = false
threadRunning = false

someMethod {
startThread(myThreadProc)
}

anotherMethod {
stopFlag = true
while threadRunning {
Sleep(250)
}
}

myThreadProc {
threadRunning = true
while not stopFlag {
doStuff()
}
threadRunning = false
}

}


Regards,

Elisa
 
A third option is to set a system event in the primary thread and have the
worker periodically check for it. Same functionality as a flag, but not
global variable.

-Chris
 
Hi,
2. set the thread object to nil (Nothing in VB).
<<

The other reason to prefer method 1 (or Chris's suggestion), is because this
doesn't work reliably (in my experience).

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
There is a simple howto that implements the global method:

http://msdn.microsoft.com/library/en-us/dncfhowto/html/stopmt.asp

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
Dominik Haneberg said:
Hi,
I'm writing an application in C# for PocketPC 2003. The application should
use a thread to read data from a bluetooth connection. If the connection is
no longer needed I want to stop and destroy the thread. As I understand the
..NET CF does not support Thread.Abort(). How can threads be terminated in
..NET CF?
 
Back
Top