开发者

How to create and call the method(Using Thread C#) in for each loop?

I wanted to call my custom method in the Thread.

 public void LoopOverAllLists(String _webAndSiteXml)
    {
        try
        {

            XmlNode _nodelist = SharePoint.ListsGetListCollection();

            foreach (System.Xml.XmlNode _item in _nodelist.ChildNodes)
            { 

                string title = _item.Attributes["Title"].Value;
                //check for hidden list
                if (_item.Attributes["Hidden"].Value.ToLower() == "false")
                {
                    switch (_item.Attributes["ServerTemplate"].Value)
                    {
                        //Check whether list is document library  
                        case SharePoint.LIST_ID_DOCUMENT_LIBRARY:
                        case SharePoint.LIST_ID_XML_FORMS:
                        case SharePoint.Publishing_ID_Pages:
                            {
                                //Get all documents info
                                try
                                {

                                    GetAllDocumentsInfo(_item, _webAndSiteXml);

                                }
                                catch
                                {

                                }

                                break;
   开发者_Python百科                         }
                        //Check whether list is having attachment
                        case SharePoint.LIST_ID_GENERIC:
                        case SharePoint.LIST_ID_ANNOUNCEMENTS:
                        case SharePoint.LIST_ID_CONTACTS:
                        case SharePoint.LIST_ID_TASKS:
                        case SharePoint.LIST_ID_EVENTS:
                        case SharePoint.LIST_ID_CUSTOM_GRID:
                        case SharePoint.LIST_ID_MEETING_SERIES:
                        case SharePoint.LIST_ID_MEETING_AGENDA:
                        case SharePoint.LIST_ID_MEETING_ATTENDEES:
                        case SharePoint.LIST_ID_MEETING_DECISIONS:
                        case SharePoint.LIST_ID_MEETING_OBJECTIVES:
                        case SharePoint.LIST_ID_MEETING_TTB:
                        case SharePoint.LIST_ID_MEETING_WS_PAGES:
                        case SharePoint.LIST_ID_PORTAL_SITE_LIST:
                            {
                                //Get all list items info having attachment 
                                try
                                {
                                    GetAllListItemsInfoOnlyAttachments(_item, _webAndSiteXml);
                                }
                                catch
                                {

                                }

                                break;

                            }
                        default:
                            GetAllListItemsInfoOnlyAttachments(_item, _webAndSiteXml);
                            break;

                    }
                    // Get All the List Forms  
                    try
                    {
                        GetAllListForms(title, _webAndSiteXml);
                    }
                    catch
                    {

                    }

                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

in above method three methods which is " GetAllDocumentsInfo , GetAllListItemsInfoOnlyAttachments and GetAllListForms " I wanted to call these function using thread in C#.

Thanks


Here is how I would approach the problem. Notice that I encapsulated the contents of the foreach loop into a separate method and then queued the execution of that method into the ThreadPool so that each iteration of the loop occurs in parallel. I also use a well established pattern to wait for all pending work items to complete. This code is compatible with .NET 3.5.

public void LoopOverAllLists(String _webAndSiteXml)
{
  int pending = 1; // Used to track the number of pending work items.
  var finished = new ManualResetEvent(false); // Used to wait for all work items to complete.
  XmlNode nodes = SharePoint.ListsGetListCollection();
  foreach (XmlNode item in nodes)
  {
    XmlNode capture = item; // This is required to capture the loop variable correctly.
    Interlocked.Increment(ref pending); // There is another work item in progress.
    ThreadPool.QueueUserWorkItem(
      (state) =>
      {
        try
        {
          ProcessNode(capture);
        }
        finally
        {
          // Signal the event if this is the last work item to complete.
          if (Interlocked.Decrement(ref pending) == 0) finished.Set();
        }
      }, null);
  }
  // Signal the event if the for loop was last work item to complete.
  if (Interlocked.Decrement(ref pending) == 0) finished.Set();
  // Wait for all work items to complete.
  finished.WaitOne();
}

private void ProcessNode(XmlNode item)
{
  // Put the contents of your loop here.
}


instead of calling

GetAllDocumentsInfo(_item, _webAndSiteXml);

use

Task.Factory.StartNew(() => GetAllDocumentsInfo(_item, _webAndSiteXml));

repeat this pattern for the other method calls as well

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜