SSOM Multi Thread Execution C# SharePoint

Amit Singh Rawat 731 Reputation points
2020-12-17T12:06:30.483+00:00

I saw many articles which says microsoft.sharepoint.dll is not thread safe.
But when I run below code via 10 to 20 parallel threads it doesn't throw any error.
Although when I tried to multi thread foreach loop within SingleThread() which is defined below. It start throwing error at "SPListItemCollection" level.

//Main method:
ParallelOptions myOptons = new ParallelOptions { MaxDegreeOfParallelism = 10 };
            int[] loop = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            Parallel.ForEach(loop, myOptons, myloop =>
             {
                 SingleThread();
             });

//Other method.

public void SingleThread()
        {
            DateTime startdate = DateTime.Now;
            try
            {

            int count = 0;
            SPWebService service = farm.Services.GetValue<SPWebService>("");
            SPWebServiceCollection webServices = new SPWebServiceCollection(farm);
            foreach (SPWebService webService in webServices) //Traverse Web Services
            {
                foreach (SPWebApplication webApp in webService.WebApplications)
                {
                    string webApplicationUrl = webApp.GetResponseUri(SPUrlZone.Default).ToString();
                    SPSecurity.RunWithElevatedPrivileges(delegate ()
                    {
                        SPWebApplication webApplication = SPWebApplication.Lookup(new Uri(webApplicationUrl));
                        foreach (SPSite sps in webApplication.Sites)
                        {

                            if (sps != null)
                            {
                                string spSiteUrl = sps.RootWeb.Url;
                                //Console.WriteLine(spSiteUrl);
                                SPWeb oWebsite = (new SPSite(spSiteUrl).OpenWeb());
                                SPWebCollection collWebsite = oWebsite.Webs;
                                foreach (SPWeb web in collWebsite)
                                {
                                    if (web != null)
                                    {

                                        foreach (SPList list in web.Lists)
                                        {
                                            if (list.BaseTemplate.ToString() == "DocumentLibrary")
                                            {
//Error in below line when I tried to multi thread SPListItemCollection.
                                                    SPListItemCollection lstitemcoll = list.GetItems();
                                                    foreach (SPListItem lstitem in lstitemcoll)
                                                    {                                                        
                                                        count++;                                                        
                                                    }
                                                }
                                            }


                                    }
                                }
                            }
                        }
                    });

                }
            }
            DateTime enddate = DateTime.Now;

            Console.WriteLine("Single Thread Count : {0}. Completed in {1} Seconds", count, (enddate - startdate).TotalSeconds);
        }
        catch (Exception ex)
        {
            string message = ex.Message;
        }
    }
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,624 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Baker Kong-MSFT 3,801 Reputation points
    2020-12-18T02:55:58.97+00:00

    Hi @Amit Singh Rawat

    SPListItemCollection/SPListCollection are not guaranteed to be thread-safe, which may throw errors in the second scenario.

    In the first scenario, the loop just invokes the task repeatedly. Its operand is int[]. The array does not take part in any calculation or get modified by the succeeding code. In this case it won't prompt thread issue.

    Best Regards,
    Baker Kong


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.