Adam K Dean

Cross thread calls made easier

Published on 27 June 2013 at 14:19 by Adam

Back in 2010 I wrote a blog post about cross thread calls, showing how to overcome invalid cross-thread operations and change windows forms from other threads. It was simple but a little bit messy.

Well, things are easier now, and have been for a while. Instead of declaring delegates and invoking them, you can now use generic delegates and save yourself the hassle of declaring new delegates for every method signature.

For example, where before you had to do this:

private delegate void ObjectDelegate(object obj);

private void UpdateTextBox(object obj)
{
    if (InvokeRequired)
    {
        ObjectDelegate method = new ObjectDelegate(UpdateTextBox);
        Invoke(method, obj);
        return;
    }
}

You can now do this:

private void UpdateTextBox(string s)
{
    if (InvokeRequired)
    {
        Action<string> action = new Action<string>(UpdateTextBox);
        Invoke(action, s);
        return;
    }
}

Another great thing is if you have to pass multiple objects then it's as simple as adding more types within the angle brackets. You can also substitute Action<T> for var to save space. Let's say for example, you need to update a label from another thread, the follow works great:

private void UpdateLabel(Label label, string s)
{
    if (InvokeRequired)
    {
        var action = new Action<Label, string>(UpdateLabel);
        Invoke(action, label, s);
        return;
    }

    label.Text = s;
}

This will save you a bunch of time, and is essentially identical to the previous method.

For more information on Action<T> Delegates, see this MSDN article.



This post was first published on 27 June 2013 at 14:19. It was filed under archive with tags csharp, delegates, generics, threading.