views:

62

answers:

1

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 :(

A: 

Oh, the problem is solved! It happens because I am subscribing to the same event several times:

Form1.GetVersionCompleted +=

The right implementation would be something like:

public delegate void OnCompleted<T>(T result, Exception ex);

    public static void GetVersionAsync(OnCompleted<string> completed)
    {
        ThreadPool.QueueUserWorkItem(o =>
        {
            try
            {
                string result = DateTime.Now.ToString();
                if (completed != null)
                {
                    completed(result, null);
                }
            }
            catch (Exception ex)
            {
                if (completed != null)
                {
                    completed(null, ex);
                }
            }
        });
    }

private void invokeButton1_Click(object sender, EventArgs e)
    {
        Form1.GetVersionAsync((string result, Exception ex) =>
            {
                this.Invoke((MethodInvoker)(() =>
                {
                    invokeText1.Text = DateTime.Now.ToString();
                }));
            });
    }

    private void invokeButton2_Click(object sender, EventArgs e)
    {
        Form1.GetVersionAsync((string result, Exception ex) =>
        {
            this.Invoke((MethodInvoker)(() =>
            {
                invokeText2.Text = DateTime.Now.ToString();
            }));
        });
    }
stkash