文字列(「ファイルを検索しています。。。。」、「選択範囲が見つかりました。。。。」など)をbackgroundWorkerとパーセンテージからwindows.formに報告するにはどうすればよいですか。さらに、backgroundWorker_Workで実行するメソッドを含む大きなクラスがあります。Class_method();で呼び出すことができます。しかし、その後、backgroundWorker_Workメソッドからのみ、呼び出されたクラスからの完了率などを報告できません。
ありがとう!
文字列(「ファイルを検索しています。。。。」、「選択範囲が見つかりました。。。。」など)をbackgroundWorkerとパーセンテージからwindows.formに報告するにはどうすればよいですか。さらに、backgroundWorker_Workで実行するメソッドを含む大きなクラスがあります。Class_method();で呼び出すことができます。しかし、その後、backgroundWorker_Workメソッドからのみ、呼び出されたクラスからの完了率などを報告できません。
ありがとう!
私は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;
}
メソッドの 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!");
}
Windows フォームでの単純なマルチスレッドを参照してください。
3部構成のシリーズです。
デリゲートを使用します。