2013-11-03 1 views
0

내 기능에 n 개의 동시 스레드를 여는 오전 :작업 처리 완료 후 MaxDegreeOfParallelism이 파일을보고 할 수 있습니까?

List<string> _files = new List<string>(); 

    public void Start() 
    { 
     CancellationTokenSource _tokenSource = new CancellationTokenSource(); 
     var token = _tokenSource.Token; 

     Task.Factory.StartNew(() => 
     { 
      try 
      { 
       Parallel.ForEach(_files, 
        new ParallelOptions 
        { 
         MaxDegreeOfParallelism = 5 //limit number of parallel threads 
        }, 
        file => 
        { 
         if (token.IsCancellationRequested) 
          return; 
         //do work... 
        }); 
      } 
      catch (Exception) 
      { } 

     }, _tokenSource.Token).ContinueWith(
      t => 
      { 
       //finish... 
      } 
     , TaskScheduler.FromCurrentSynchronizationContext() //to ContinueWith (update UI) from UI thread 
     ); 
      } 

이 기능이 여전히 1 개 파일 마감을 처리하는 동안 알 수있는 방법이 있나요? 나는 지금 ContinueWith에 대해 이야기하고 있는데, 내 모든 list이 끝나면이 경우입니다. (예 : UI를 업데이트하는 일부 공유 상태를 업데이트 할 경우 추가 잠금이 필요할 수 있습니다

public void Start() 
{ 
    CancellationTokenSource _tokenSource = new CancellationTokenSource(); 
    var token = _tokenSource.Token; 

    Task.Factory.StartNew(() => 
    { 
     try 
     { 
      Parallel.ForEach(_files, 
       new ParallelOptions 
       { 
        MaxDegreeOfParallelism = 5 //limit number of parallel threads 
       }, 
       file => 
       { 
        if (token.IsCancellationRequested) 
         return; 
        //do work... 
        OnDone(file); 
       }); 
     } 
     catch (Exception) 
     { } 

    }, _tokenSource.Token).ContinueWith(
     t => 
     { 
      //finish... 
     } 
    , TaskScheduler.FromCurrentSynchronizationContext() //to ContinueWith (update UI) from UI thread 
    ); 
} 

public void OnDone(string fileName) 
{ 
    // Update the UI, assuming you're using WPF 
    someUIComponent.Dispatcher.BeginInvoke(...) 
} 

, 만에 :

답변

0

나는 완벽하게 문제를 확실하지 이해하지만 당신은 방법을 통해 몇 가지 표준 알림을 사용할 수 있습니다 데이터 그리드의 행 또는 목록의 요소)은 디스패처 호출에 의해 동기화가 적용되므로 정상이어야합니다.

관련 문제