Nick Devereaux
Nick Devereaux

Reputation: 833

Whats the best way to close a WCF thread?

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;

Upvotes: 2

Views: 972

Answers (2)

GrahamS
GrahamS

Reputation: 10350

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

Upvotes: 5

Brandon
Brandon

Reputation: 14196

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.

Upvotes: 3

Related Questions