我找不到这个问题的解决方案,这里是一个简化的例子:在一个 Windows 窗体上,我有 2 个文本框(invokeText1、invokeText2)和两个按钮(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();
}
两者都调用异步方法:
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));
}
});
}
单击单个按钮时,它仅更新其相关的文本框。单击两个按钮时,每个按钮都会更新两个文本框。
我认为应该有一些简单的东西,但我找不到什么:(