views:

332

answers:

2

I have an application which is taking ages to close. When I close the app, it tries to dispose a number of threads that do TCP scanning, WCF P2P attempts, and so on. The problem lies in a WCF thread that stalls on a method for about 17 seconds.

IP2PAuthenticationService server;
ChannelFactory<IP2PAuthenticationService> channelFactory;
channelFactory = new ChannelFactory<IP2PAuthenticationService>(binding, endpointAddress);

server = channelFactory.CreateChannel();
string result = server.SendMyDetails(myContract, "foo");

So all this happens inside a thread. When the form closes it attempts to dispose the thread

 if (prospectCrawlerThread != null)
 {
      prospectCrawlerThread.Abort();
      //prospectCrawlerThread.Join();
      prospectCrawlerThread = null;
  }

I've confirmed this by uncommenting the .Join(), and also by pausing the debug and seeing the threads that are still running.

What's the best way to get rid of this thread?

Edit: setting the thread to background seemed to make it quicker prospectCrawlerThread.IsBackground = true;

+2  A: 

IsBackgroundThread = true will abort the thread automatically when the form closes, so I think that's what you're looking for if you just want to kill it.

routeNpingme
This best practice for sockety threads?
alphabeat
It will get the job done, however, I would build in some way of closing sockets on the FormClosing event of the application. Especially if you're the server side.
routeNpingme
+3  A: 

It is worth pointing out that Thread.Abort is generally a bad practise and should be avoided:

"aborting a thread is pure evil. Try to never do so!"
   -- Eric Lippert, Fabulous Adventures In Coding

"Thread.Abort is a Sign of a Poorly Designed Program"
   -- Peter Ritchie's MVP Blog

GrahamS