2012-01-15 1 views
1

저는 다른 개발자와 같이 얄팍한 개발자입니다. 왜 BackgroundWorker를 사용할지 정적 메서드로 작성하려고합니다. 이 메서드는 불어 :함수 매개 변수로 BackgroundWorker.ReportProgress()를 호출하십시오.

public static void RunInThread(DoWorkEventHandler method 
            ,ProgressChangedEventHandler progressHandler 
            ,RunWorkerCompletedEventHandler completed) 
    { 
     var bkw = new BackgroundWorker(); 
     bkw.DoWork += method; 
     bkw.ProgressChanged += progressHandler; 
     bkw.RunWorkerCompleted += completed; 
     bkw.WorkerReportsProgress = true; 
     bkw.RunWorkerAsync(); 
    } 

그러나 내가 BackgroundWorker.ReportProgress 메서드를 사용하는 데 문제가 있습니다. 나를 도와 줄 사람이 여기 있니? TNX

+0

무슨 문제가 있습니까? – annonymously

답변

0

무엇 대신 RunInThread에서 void을 반환하는 당신이 하리스의 솔루션이 허용하는 동안 backgroundWorker.ReportProgress();

0

전화 instace 당신의 DoWork 사용 후 BackgroundWorker 클래스 (BKW)과의 객체를 반환하는 경우 "빠른 수정" 당신은 당신의 정적 방법에 대한 주요 디자인 결함이 있습니다; BackgroundWorker 클래스는 Component이며 IDisposable을 구현하므로 수명이 다한 시점에 처분해야합니다.

패턴을 다시 만드는 가장 좋은 방법은 수동으로 다른 스레드에서 작업 함수를 호출하는 것입니다. 이에 대한 유일한 단점은 이어야하며 진행을 업데이트하는 동안 UI 스레드의 모든 항목과 상호 작용하려면 Dispatcher.Invoke으로 전화해야합니다. 어떻게 할 수 있는지에 대한 샘플이 있습니다.

using System.Threading; 

public static void RunInThread<TProgress>(
    Action<Action<TProgress>> worker, 
    Action<TProgress> updateProgress, 
    Action workerCompleted) 
{ 
    Thread workerThread = new Thread(() => 
    { 
     worker(updateProgress); 
     workerCompleted(); 
    }); 

    workerThread.Start(); 
} 

일반적인 방법이기 때문에 원하는대로 진행 상황을보고 할 수 있습니다. 또한 단일 매개 변수 또는 반환 값으로 작업 할 때 편리 성을 추가 과부하를 만드는 아주 쉽게이 방법을 사용자 정의 할 수 있습니다

RunInThread<double>(
    updateProgress => 
    { 
     Thread.Sleep(500); 
     updateProgress(0.5); 

     Thread.Sleep(500); 
     updateProgress(1); 
    }, 
    progress => 
    { 
     this.Dispatcher.Invoke(() => 
     { 
      progressLabel.Text = progress.ToString(); 
     }); 
    }, 
    () => 
    { 
     this.Dispatcher.Invoke(() => 
     { 
      progressLabel.Text = "Finished!"; 
     }); 
    } 
); 

: 여기 당신이 그것을 호출 할 수있는 방법의 예입니다.

public static void RunInThread<TProgress, TParameter>(
    Action<Action<TProgress>, TParameter> worker, 
    Action<TProgress> updateProgress, 
    Action workerCompleted, 
    TParameter parameter) 
{ 
    Thread workerThread = new Thread(() => 
    { 
     worker(updateProgress, parameter); 
     workerCompleted(); 
    }); 

    workerThread.Start(); 
} 

public static void RunInThread<TProgress, TResult>(
    Func<Action<TProgress>, TResult> worker, 
    Action<TProgress> updateProgress, 
    Action<TResult> workerCompleted) 
{ 
    Thread workerThread = new Thread(() => 
    { 
     TResult result = worker(updateProgress); 
     workerCompleted(result); 
    }); 

    workerThread.Start(); 
} 

public static void RunInThread<TProgress, TParameter, TResult>(
    Func<Action<TProgress>, TParameter, TResult> worker, 
    Action<TProgress> updateProgress, 
    Action<TResult> workerCompleted, 
    TParameter parameter) 
{ 
    Thread workerThread = new Thread(() => 
    { 
     TResult result = worker(updateProgress, parameter); 
     workerCompleted(result); 
    }); 

    workerThread.Start(); 
} 

업무 기능에서 문제가 발생할 경우를 대비하여 오류 처리 코드를 추가하는 것이 좋지만이 연습을 끝내도록하겠습니다.

+0

http://stackoverflow.com/a/2542380/17034 –

관련 문제