2011-05-09 3 views
1

스레드에서 내 사용자 지정 메서드를 호출하고 싶었습니다. 상기 방법에각 루프마다 (Thread C# 사용) 메소드를 만들고 호출하는 방법은 무엇입니까?

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; 
          } 
         //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; 
     } 
    } 

는 "GetAllDocumentsInfo, GetAllListItemsInfoOnlyAttachments 및 GetAllListForms"인 세 가지 방법 I는 C#으로 나사를 사용하여이 함수를 호출 원했다.

감사 대신 다른 방법에 대한

GetAllDocumentsInfo(_item, _webAndSiteXml); 

사용을

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

반복이 패턴을 호출

답변

1

내가 문제를 접근 할 방법입니다뿐만 아니라 호출합니다. foreach 루프의 내용을 별도의 메서드로 캡슐화 한 다음 해당 메서드의 실행을 ThreadPool에 대기시키기 때문에 루프의 각 반복이 병렬로 발생합니다. 또한 보류중인 모든 작업 항목이 완료 될 때까지 기다리기 위해 잘 설정된 패턴을 사용합니다. 이 코드는 .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. 
} 
1

여기

+0

Task.Factory에는 스레드에 참여하기위한 뮤텍스 또는 조인 키워드가 없습니다. –

+0

어떻게이 함수를 .net framwork 3.5에서 사용할 수 있습니까? –

+0

나는 그가 묻지 않았기 때문에 동기화 문제를 언급하지 않기로 결정했다. 그 사람도 그 문제를 해결할 필요가있을 것이라고 확신합니다. .NET 4의 Task 라이브러리는 아마도 그가 갈 필요가있는 곳으로 가져갈 수 있습니다. – kenwarner

관련 문제