0

I have a thread array in which each thread gets a search task from a thread search manager - using a producer/consumer module - and when it finishes it, it waits until the next signal from the search manager thread. The search is performed via web - and it takes a significant amount of time (mainly the getting the html data part). My problem is when the user selects to stop the current searches and to start a new set of searches, all the searches are busy during the previous web search, so currently, the user waits until the no-longer-relevant searches end their task and begin the new one.

My question is - how can I start immediately with the new relevant task? I couldn't use a flag (to indicate the thread to skip the search) since the long wait is for the get http method. Is there a way to stop aggressively the method and to start it from the beginning? (I prefer not using new thread...)

Thanks in advance,

Shmouel.

Shmouel
  • 3
  • 3
  • 2
    *Why* do you prefer not using a new thread? That's the most obvious option. Set a flag to tell the existing operation that it's no longer required, and start a new thread. – Jon Skeet May 19 '11 at 08:47
  • 2
    Dedicating threads to operations that are IO bound is going to have excessive overheads. Better to use tasks, which already include support for requesting cancellation (via [`CancelationToken`](http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken.aspx). That way you avoid all need to implement and maintain your own thread pool. Using as much asynchronous IO as posssible in the task implementation would minimise the number of threads in use and thus improve the performance of your application. – Richard May 19 '11 at 08:53
  • If you have access to .NET 4, the Task class and cancellation tokens can be used to accomplish this. – Brian Rasmussen May 19 '11 at 08:54
  • First of all - thanks for the .net 4 suggestion. Unfortunately, I'm using .net 3.5. @Jon - If i'll use new thread - each time the user will select new search will result in new threads which will lead to numerous threads that will run simultaneously although non of them is relevant. Any other suggestions? – Shmouel May 19 '11 at 08:56
  • They wouldn't really be actively taking CPU simultaneously for long, if you check the flag regularly within the thread. Sure, you may be fetching an HTTP resource in several threads, but is that really so bad? What are the negative impacts you're trying to avoid? – Jon Skeet May 19 '11 at 09:12

1 Answers1

0

I found a similar stackoverflow thread that should be of use. The trick is to use BeginGetResponse instead of GetResponse in order to call an abort on the HTTP request: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.abort.aspx

Link to original thread: Killing HttpWebRequest object using Thread.Abort

Community
  • 1
  • 1
BrandonK.
  • 128
  • 3
  • 11
  • @user680811 I also cannot change (at least not without serious code changes :-)) the http request method... However, if I won't have any other option, I'll use that. Thanks! – Shmouel May 19 '11 at 09:15