I am working on an ASP.Net MVC 3 C# project.
I have read about ThreadPool.QueueUserWorkItem and Tasks, I think what i need should use one of them.
public static void ProcessDay(DateTime day) { ... }
Let’s say that I want to process days from 2012-01-01 to 2012-01-31, So I have to call ProcessDay function 31 times.
How to to process 5 days at once, and keep running more threads as any of the 5 threads is done, till it finishes the 31 days?
Samples are highly appreciated, and considering showing progress bar is a good idea 🙂
A simple parallelization of this could be:-
(Note that, if you have shared state that each thread needs to modify, then you will have to use thread synchronization techniques appropriately.)
As for the number of threads, it’s usually best to leave
Parallel.ForEach()to work this out for itself. It will use the thread pool internally and will use all available processors efficiently. You can limit the number of threads it will use by using an overload ofForEachwhich takes aParallelOptionsobject withMaxDegreeOfParallelismset to the required amount, but you should run some benchmarks to see whether you can really achieve better results by doing this.Moreover, if this is a ‘long running task’ for which you wish to show progress, then you won’t be able to achieve this with a single web request. The notion of a ‘progress bar’ in a website is rather different to that in a desktop application. In a website this typically has to be done by polling. I.e. send a request which starts the process (response returned immediately), poll periodically for progress (which can be displayed in a progress bar or similar) and then display the result when finished.
There are also frameworks and techniques being developed for ‘push’ type notification e.g. SignalR, so this may also be an option if you want to investigate it.