I cannot find the solution for this problem, here is the simplified example: On a windows form I have 2 text boxes (invokeText1, invokeText2) and two buttons (invokeButton1, invokeButton2). There are both button clicks:
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();
}
Both call to the async method:
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));
}
});
}
When a single button clicked, it updates its related text box only. When both buttons clicked, each updates both text boxes.
I think there should be something simple, but I cannot find what :(