2011-08-17 7 views
1

내 WPF 응용 프로그램에 대한 진행률 막대를 추가하고 있습니다. 4/100 파일 등을 생성처럼 다음은 이러한 작업WPF 실시간 진행률 막대

 Action Generate = new Action(() => 
      { 
       foreach (string file in Files) 
       {     

      //all the logic to generate files 


        using (var streamWriter = new StreamWriter(newFileName, false, Encoding.Default)) 
        { 
         foreach (string segment in newFile) 
         { 
          streamWriter.WriteLine(segment); 
         } 
         filesGenerated++; 

       //I need to do the second action here      
        } 

       } 
      }); 


    Action ShowProgressBar = new Action(() => 
      { 
       progressBar.Value = filesGenerated 
    lblProgress.Content = filesGenerated + " File(s) Generated."; 

      }); 

    Task GenerateTask = Task.Factory.StartNew(() => Generate()); 

    Task ShowProgressBarTask = new Task(ShowProgressBar); 
실행하는 두 가지 작업과 작업이 실시간으로 생성 된 파일의 수와 함께 진행 표시 줄에 실시간 진행 상황을 보여주고 싶은

작업을 중첩 시키려고했지만 작동하지 않습니다. 진행률 표시 줄에 실시간 진행 상황을 표시하려면 어떻게해야합니까?

+0

가 확장 WPF 툴킷을 사용하십시오, 그것은 BusyIndicator 요소가 ] (http://wpftoolkit.codeplex.com/wikipage?title=BusyIndicator&referringTitle=Home) – Rumplin

+0

타사 구성 요소에 연결해도 코드 문제를 해결하는 데 도움이되지 않습니다. 진행 상황을 보여주기 위해 툴킷이 필요하지 않습니다! –

+0

이 WPF Toolkit은 Silverlight 응용 프로그램을 빌드하는 데 필수적인 Silverlight Toolkit과 동일합니다 ... – Rumplin

답변

3
  1. 다른 스레드에서 당신 원인 Dispatcher를 사용하여 foreach 루프에서 진행률 표시 줄의 값을 설정 다른 스레드

  2. 에 파일 생성 코드를 실행하고 하나의 UI 컨트롤을 변경할 수 없습니다.

기본적으로 완료되었습니다.

0

내 답변을 BackgroundWorker herehere에서 살펴보십시오.

기본적으로 파일 작성을 다른 스레드로 옮기고 BackgroundWorker의 메서드와 이벤트를 사용하여 UI 스레드에서 진행 상태를보고합니다. 메인 코드에서

-1
public static class Refresh 

    { 
     public static void Refresh_Controls(this UIElement uiElement) 
     { 
      uiElement.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { })); 
     } 
    } 

: [CodePlex의 프로젝트 (http://wpftoolkit.codeplex.com/) [BusyIndicator 문서 :

Refresh.Refresh_Controls(progressBar1);