C# WinForms – How to Update a Windows Form GUI from Another Class

c++multithreadingwinforms

how do you update a win forms control (ex. label, progress bar) from another class which created the GUI but not in the creating thread? (for example, an event handler of Program.cs)

I've found several posts regarding updating GUI from another thread using Invoke() method, but what i've found so far only works if the codes are written in the same class as the form

http://www.yoda.arachsys.com/csharp/threads/winforms.shtml

Best Answer

You need to call the Invoke method on the form. For example:

private void someMethod() {
    myOtherForm.Invoke(new MethodInvoker(delegate() {
        // update things in myOtherForm here
    }));
}

If you don't need the updates to be finished before returning back to the method, you should use BeginInvoke instead of Invoke.