I have built an ASP.Net application that interact with CRM 2011 on premise from Azure worker role. Now it is a single threaded application. I want to implement multithreading so that other process in the queue also gets processed asynchronously. Could you please suggest what is the best way to do it and how it can be done. Any references would be highly appreciated.
2 Answers
0
votes
0
votes
I've never used the Azure Worker Role but from my quick googling, there is no reason the code below shouldn't work. I use this for batch processes for my onsite CRM.
var options = new ParallelOptions();
// For single Thread debugging, set MaxDegreeOfParallelism to 1:
options.MaxDegreeOfParallelism = 10; // Make this value configurable you can slow down CRM if you try to pump too many things through it.
var actions = GetListOfWorkItems().Select(workItem => (Action)(() => ProcessWorkItem(workItem)));
Parallel.Invoke(options, actions.ToArray());
private void ProcessWorkItem(WorkItem workItem){
// Code to run in parallel goes here
}
Just a few notes
- The IOrganization is not multi-thread safe, so create one for each thread that needs it
- If you're processing thousands of records, it's usually more efficient to batch the records up, so thread 1 opens a connection, and processes items 1-1000, thread 2 opens a connection and processes items 10001-2000 etc...
- All multi-threading rules apply, any shared data will need to have a safeguard to ensure it's multi-thread safe.