web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

C# Parallel.ForEach, not working as expected.

(0) ShareShare
ReportReport
Posted on by 5

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)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans