11

文字列(「ファイルを検索しています。。。。」、「選択範囲が見つかりました。。。。」など)をbackgroundWorkerとパーセンテージからwindows.formに報告するにはどうすればよいですか。さらに、backgroundWorker_Workで実行するメソッドを含む大きなクラスがあります。Class_method();で呼び出すことができます。しかし、その後、backgroundWorker_Workメソッドからのみ、呼び出されたクラスからの完了率などを報告できません。

ありがとう!

4

4 に答える 4

25

私はWCFにもメソッドがあると仮定しています

public void ReportProgress(int percentProgress, Object userState); 

したがって、userState を使用して文字列を報告するだけです。

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
 //report some progress
 e.ReportProgress(0,"Initiating countdown");

// initate the countdown.
}

そして、「カウントダウンを開始しています」という文字列が ProgressChanged イベントに返されます。

private void worker_ProgressChanged(object sender,ProgressChangedEventArgs e) 
{
  statusLabel.Text = e.UserState as String;
}
于 2009-08-17T17:50:07.120 に答える
9

メソッドの userState パラメータを使用して、ReportProgressその文字列を報告できます。

MSDN の例を次に示します。

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // This method will run on a thread other than the UI thread.
    // Be sure not to manipulate any Windows Forms controls created
    // on the UI thread from this method.
    backgroundWorker.ReportProgress(0, "Working...");
    Decimal lastlast = 0;
    Decimal last = 1;
    Decimal current;
    if (requestedCount >= 1)
    { AppendNumber(0); }
    if (requestedCount >= 2)
    { AppendNumber(1); }
    for (int i = 2; i < requestedCount; ++i)
    {
        // Calculate the number.
        checked { current = lastlast + last; }
        // Introduce some delay to simulate a more complicated calculation.
        System.Threading.Thread.Sleep(100);
        AppendNumber(current);
        backgroundWorker.ReportProgress((100 * i) / requestedCount, "Working...");
        // Get ready for the next iteration.
        lastlast = last;
        last = current;
    }

    backgroundWorker.ReportProgress(100, "Complete!");
}
于 2009-08-17T17:41:51.580 に答える
4

Windows フォームでの単純なマルチスレッドを参照してください。

3部構成のシリーズです。

于 2009-08-17T17:36:44.243 に答える
0

デリゲートを使用します。

于 2009-08-17T17:32:59.073 に答える