2

時間がかかるプロセスがあり、進行状況をウィンドウに表示したい。しかし、進捗状況を表示する方法がわかりません。

コードは次のとおりです。

if (procced)
{
    // the wpf windows :
    myLectureFichierEnCour = new LectureFichierEnCour(_myTandemLTEclass);
    myLectureFichierEnCour.Show();

    bgw = new BackgroundWorker();
    bgw.DoWork += startThreadProcessDataFromFileAndPutInDataSet;
    bgw.RunWorkerCompleted += threadProcessDataFromFileAndPutInDataSetCompleted;

    bgw.RunWorkerAsync();
}

と:

private void startThreadProcessDataFromFileAndPutInDataSet(object sender, DoWorkEventArgs e)
{
    _myTandemLTEclass.processDataFromFileAndPutInDataSet(
        _strCompositeKey,_strHourToSecondConversion,_strDateField);
}

_myTandemLTEclass.processProgress進行状況のヒントを得るために電話することができます。

4

2 に答える 2

6

イベントを処理し、ProgressChangedそこでユーザー インターフェイスの進行状況バーを更新する必要があります。

作業を行う実際の関数 (DoWorkイベント ハンドラー) では、完了したタスクの量を指定する引数を使用しReportProgressてインスタンスのメソッドを呼び出します。BackgroundWorker

MSDN ライブラリのBackgroundWorker の例は、ジョブを実行する単純なコード スニペットです。

于 2009-12-03T21:26:54.927 に答える
1

backgroundWorker スレッドは、DoWorkメソッドとを処理する必要がありProgressChangedます。

WorkerReportsProgressまた、フラグを true (デフォルトではオフ) に設定する必要があります。

サンプルコードを参照してください:

private void downloadButton_Click(object sender, EventArgs e)
{
    // Start the download operation in the background.
    this.backgroundWorker1.RunWorkerAsync();

    // Disable the button for the duration of the download.
    this.downloadButton.Enabled = false;

    // Once you have started the background thread you 
    // can exit the handler and the application will 
    // wait until the RunWorkerCompleted event is raised.

    // Or if you want to do something else in the main thread,
    // such as update a progress bar, you can do so in a loop 
    // while checking IsBusy to see if the background task is
    // still running.

    while (this.backgroundWorker1.IsBusy)
    {
        progressBar1.Increment(1);
        // Keep UI messages moving, so the form remains 
        // responsive during the asynchronous operation.
        Application.DoEvents();
    }
}
于 2009-12-03T21:31:09.660 に答える