C# Windows Forms Application – Updating GUI from another thread AND class?

Check the InvokeRequired of the Control class, and if it’s true, then call the Invoke and pass in a delegate (usually an anonymous method) that does what you want to do on the client’s thread.

Example:

public void DoWork(Form form)
{
    if (form.InvokeRequired)
    {
        // We're on a thread other than the GUI thread
        form.Invoke(new MethodInvoker(() => DoWork(form)));
        return;
    }

    // Do what you need to do to the form here
    form.Text = "Foo";
}

Leave a Comment