How to Kill ( Dispose ) a Thread

A

Anders Fredborg

Hi !

I have developed an application running on a PDA with PPC2003. The Main Form
starts a Thread that is supposed to run concurrently with the main Thread.
This works Superb. However, when i close the application, it doesnt seem
like the Thread is being Disposed correctly, since I cant restart the
application, until i soft-reset the PDA.

This is how the Object is created, and the thread started in the Main Form:


myRFnetTool = new myRFUtils();

System.Threading.Thread thr = new Thread(new
ThreadStart(myRFnetTool.updateAccessPointsList));

thr.Priority = System.Threading.ThreadPriority.Normal;

thr.Start();



Here is the updateAccessPointsList() procedure of the MyRFNetTool



public void updateAccessPointsList()

{

while(!isDisconnecting)

{

// Here some code is performed

System.Threading.Thread.Sleep(3000);

updateAccessPointsList(); // The method is called recursively
with 3 sec intervals, until the Global Variable "isDisconnecting" is set to
true, which is set at the Objects Dispose function

}


}

How can i make sure the Thread gets disposed more properly? It doesnt seem
to get disposed properly, since i cant restart the Application:

Plz Help.

Regards
Anders
 
G

Guest

..NET will dispose of all background threads when a
process terminates. To solve your problem, set
thr.IsBackground=true;
and your secondary thread will be killed when the
application closes.
Hope this helps,
Aleksey Nudelman
http://csharpcomputing.com
-----Original Message-----
Hi !

I have developed an application running on a PDA with PPC2003. The Main Form
starts a Thread that is supposed to run concurrently with the main Thread.
This works Superb. However, when i close the application, it doesnt seem
like the Thread is being Disposed correctly, since I cant restart the
application, until i soft-reset the PDA.

This is how the Object is created, and the thread started in the Main Form:


myRFnetTool = new myRFUtils();

System.Threading.Thread thr = new Thread(new
ThreadStart(myRFnetTool.updateAccessPointsList));

thr.Priority = System.Threading.ThreadPriority.Normal;

thr.Start();



Here is the updateAccessPointsList() procedure of the MyRFNetTool



public void updateAccessPointsList()

{

while(!isDisconnecting)

{

// Here some code is performed

System.Threading.Thread.Sleep(3000);

updateAccessPointsList(); // The method is called recursively
with 3 sec intervals, until the Global
Variable "isDisconnecting" is set to
 
C

Chris Tacke, eMVP

Not true in the CF. You must manually signal the thread to exit before
exiting your application.

-Chris
 
G

Ginny Caughey [MVP]

Aleksey,

This is true for the .Net Framework but not for the Compact Framework. It's
still better programming on both platforms to have threads end themselves
before the process ends to ensure that everything is left in a known state.
 
G

Guest

This is one of my top three complaints about the CF. 1.) You cannot abort
threads and 2.) There is no stack trace and 3.) you cannot attach a specified
document to an application email from inside MyApp.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top