0

In my application I am taking a range of IP addresses and then splitting them up until smaller ranges and creating a thread for each smaller range, and then executing a DoWork event that contains a loop, Which loops through each IP in the smaller range and does some work.

I wan't to be able to click a button to pause all work, and then be able to resume it when I click it again.

To stop the operation, I am guessing I will need to terminate all threads, although I am unsure how I could do so. Can anyone guide me on the right track?

Here is my code to start the operation and then the DoWork Method.

user1632018
  • 2,485
  • 10
  • 52
  • 87
  • Some ideas to get you started would be `System.Threading.Tasks.Task` and `System.Threading.CancellationTokenSource`. – mdisibio Apr 25 '14 at 23:34
  • I actually have created an implementation using tasks instead of threads. It isn't too viable in my situation and actually slows down performance compared to threads alone. – user1632018 Apr 26 '14 at 01:09
  • Regarding the last commment, I can't see how tasks could slow down performance, I believe there was something wrong with your code. [Here](http://stackoverflow.com/q/19613444/1768303) is how you could do it with tasks. With bare threads, you could use a pair of `ManualResetEvent` to implement pause/resume. – noseratio Apr 26 '14 at 02:01
  • @Noseratio Tasks alone are not capable of handling what I am looking for. I need to run up to 2.5 million requests, which use sockets, and other complex objects. Running benchmarks, if I did it with tasks it would take me days. If I did it with threads it will take me around 9 hours. – user1632018 Apr 26 '14 at 03:08
  • @Noseratio Tasks in almost every use case would be ideal but when running a request like this tasks would take alot longer, which isn't acceptable in my situation. – user1632018 Apr 26 '14 at 03:25
  • @user1632018, tasks use `ThreadPool`, the thread pool has its limits. You always can change the size of `ThreadPool` with `ThreadPool.SetMaxThreads`/`ThreadPool.SetMinThreads`. Alternatively, start every task with `Task.Factory.StartNew(..., TaskCreationOption.LongRunning)`, you won't be limited to the size of thread pool, and it should give you a behavior similar to `new Thread`. That said, spawning a huge number of threads is probably a sign of bad design. You should rather use naturally async socket API, etc. – noseratio Apr 26 '14 at 03:29
  • If you're using .NET 4.0 or later, use [Cancellation](http://msdn.microsoft.com/en-us/library/dd997364(v=vs.110).aspx). Prior to .NET 4.0, use a ManualResetEvent. See [Polling for cancellation](http://blog.mischel.com/2013/05/07/polling-for-cancellation/). You can extend it to do pause/resume. – Jim Mischel Apr 26 '14 at 04:11
  • 1
    @Noseratio Using TaskCreationOption.LongRunning seems to have made a huge improvement. My issue was that I was periodically receiving long delays. This seems to have fixed that. – user1632018 Apr 26 '14 at 04:18

0 Answers0