Archived

This topic is now archived and is closed to further replies.

Trashcann

.NET Thread refuses to abort

Recommended Posts

I've got a thread started,
_listenthread = new Thread(new ThreadStart(WaitForConnection));
_listenthread.Start();
  
and that thread does this
_socket = _tcplistener.AcceptSocket();
_timeoutcount = 0;
ThrowConnected();
_thread = new Thread(new ThreadStart(RunThread));
_thread.Start();
  
What's wrong is that no matter what I do, I can't make this thread stop. I called _listenthread.Abort(), _listenthread = null, Application.ExitThread() and tried just Application.Exit() But whatever I do, when I close my form the application keeps running. It only works if I comment out _socket = _tcplistener.AcceptSocket(); so I know it has to do with that. Anybody know how to abort this process correctly? EDIT: ok, I figured it out. All I had to do was set _listenthread.IsBackground = true; Sorry for the trouble. [edited by - Trashcann on April 3, 2004 2:39:13 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
IsBackground isn''t a good solution, there is a better way:

The thread cannot be aborted since it´s in an unmanaged call to winsock. Your main thread should close the socket instead. The listener thread will then get an exception in the accept call.

Share this post


Link to post
Share on other sites