What is the main difference between two of following approaches:
ThreadPool.QueueUserWorkItem
Clients objClient = new Clients();
List<Clients> objClientList = Clients.GetClientList();
foreach (var list in objClientList)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(SendFilesToClient), list);
}
System.Threading.Tasks.Parallel ForEach
Clients objClient = new Clients();
List<Clients> objClientList = Clients.GetClientList();
Parallel.ForEach<Clients>(objClientList, list =>
{
SendFilesToClient(list);
});
I am new to multi-threading and want to know what's going to happen in each case (in terms of execution process) what's the level of multi-threading for each approach? Help me visualize both the processes.
SendFilesToClient: Gets data from database, converts to Excel and sends the Excel file to respective client.
Thanks!
The main difference is functional.
Parallel.ForEachwill block (by design), so it will not return until all of the objects have been processed. Yourforeachqueuing threadpool thread work will push the work onto background threads, and not block.Also, the
Parallel.ForEachversion will have another major advantages - unhandled exceptions will be pushed back to the call site here, instead of left unhandled on a ThreadPool thread.In general,
Parallel.ForEachwill be more efficient. Both options use the ThreadPool, butParallel.ForEachdoes intelligent partitioning to prevent overthreading and to reduce the amount of overhead required by the scheduler. Individual tasks (which will map to ThreadPool threads) get reused, and effectively "pooled" to lower overhead, especially ifSendFilesToClientis a fast operation (which, in this case, will not be true).Note that you can also, as a third option, use PLINQ:
This will be very similar to the
Parallel.ForEachmethod in terms of performance and functionality.