
Hi All,
In my console app facing a strange issue with Parallel Foreach loop, I looping through one entitycollection i.e. Poll and calling one api to get url, and one function getTemplateDiscription to get the various language contents. The issue is when I am running it with single thread it works properly but when I am running with multiple thread, content is not getting set for all records i.e. some content is getting set as null. But working fine with single thread. Following is the code snippet:-
//Global variable - Outside the Parallel Foreach.
//Guid contactId, templateIdEnglish, templateIdEnglishReminder, templateIdGerman, templateIdGermanReminder
Parallel.ForEach<acn_poll, OrganizationServiceProxy>(pollList, new ParallelOptions() { MaxDegreeOfParallelism = maxDegreeOfParallelism },
() =>
{
return CreateServiceProxy();
}, (poll, loopstate, index, threadProxy) =>
{
//Logic to call api
var content1 = getTemplateDescription(contactId, templateIdEnglish, threadProxy);
var content2 = getTemplateDescription(contactId, templateIdEnglishReminder, threadProxy);
var content3 = getTemplateDescription(contactId, templateIdGerman, threadProxy);
var content4 = getTemplateDescription(contactId, templateIdGermanReminder, threadProxy);
//update poll entity with Content
}
public static string getTemplateDescription(Guid contactId, Guid templateId, string entityName, string shortenUrl, IOrganizationService service)
{
if (templateId == Guid.Empty)
return "";
string content = string.Empty;
InstantiateTemplateRequest instTemplateReq = new InstantiateTemplateRequest
{
TemplateId = templateId,
ObjectId = contactId,
ObjectType = entityName
};
InstantiateTemplateResponse instTemplateResp = (InstantiateTemplateResponse)service.Execute(instTemplateReq);
if (instTemplateResp != null)
{
Entity template = instTemplateResp.EntityCollection.Entities[0];
if (template != null && template.Attributes.Contains("description"))
{
content = template.Attributes["description"].ToString();
}
}
return content;
}
In debug mode I am unable to find the root cause, can somebody point out the mistake?
Thanks
*This post is locked for comments
I have the same question (0)