0

同期とスレッドの使用に関するプログラムを 1 つ見ました。プログラムのある部分で、このラムダ式を見て混乱しました。

for (int i = 0; i < 100; i++)
{
  Thread.Sleep(100);
  progressBar1.BeginInvoke(new Action(() =>
  {
    progressBar1.Value = i;
    listBox1.Items.Add(i.ToString());
  }));
}

私の質問は、なぜこのラムダ式に入力引数がないのですか?

4

2 に答える 2

1

Control.BeginInvokeメソッドの説明には、次のように書かれています。

// Summary:
//     Executes the specified delegate asynchronously on the 
//     thread that the control'sunderlying handle was created on.
//
// Parameters:
//   method:
//     A delegate to a method that takes no parameters.

Action Delegate MS の記事から:

パラメーターを持たず、値を返さないメソッドをカプセル化します。

于 2013-07-17T09:10:33.977 に答える
1

ここでの目標は、UI スレッドで UI にアクセスするいくつかのステートメントを実行することです。アクションは入力を必要とせず (とiがキャプチャされます)、出力もありません。副作用 (UI の変更) のみです。progressBar1listBox1

于 2013-07-17T09:27:00.697 に答える