この問題の解決策が見つかりません。簡単な例を次に示します。Windows フォームには、2 つのテキスト ボックス (invokeText1、invokeText2) と 2 つのボタン (invokeButton1、invokeButton2) があります。両方のボタンクリックがあります:
private void invokeButton1_Click(object sender, EventArgs e)
{
Form1.GetVersionCompleted += (object sender1, AsyncCompletedEventArgs e1) =>
{
this.Invoke((MethodInvoker)(() =>
{
invokeText1.Text = DateTime.Now.ToString();
}));
};
Form1.GetVersionAsync();
}
private void invokeButton2_Click(object sender, EventArgs e)
{
Form1.GetVersionCompleted += (object sender1, AsyncCompletedEventArgs e1) =>
{
this.Invoke((MethodInvoker)(() =>
{
invokeText2.Text = DateTime.Now.ToString();
}));
};
Form1.GetVersionAsync();
}
どちらも async メソッドを呼び出します。
public static event EventHandler<AsyncCompletedEventArgs> GetVersionCompleted;
public static void GetVersionAsync()
{
ThreadPool.QueueUserWorkItem(o =>
{
try
{
string result = DateTime.Now.ToString();
GetVersionCompleted(
null,
new AsyncCompletedEventArgs(
null,
false,
result));
}
catch (Exception ex)
{
GetVersionCompleted(
null,
new AsyncCompletedEventArgs(
ex,
false,
null));
}
});
}
1 つのボタンをクリックすると、関連するテキスト ボックスのみが更新されます。両方のボタンをクリックすると、それぞれが両方のテキスト ボックスを更新します。
シンプルなものがあるはずだと思いますが、何が見つかりません:(