I keep seeing on various forums, tutorials or SO answers a recommendation to run the UI on a separate thread then the rest of the program to keep it responsive.
How is this done in practice? Does it mean editing program.cs to start a thread before loading the form? Or does it mean that any non-trivial operation activated from within the form fork a thread and use that? Or is it some setting? How do you work with it??
I keep seeing on various forums, tutorials or SO answers a recommendation to run
Share
To keep UI responsive you should run other time consuming operations in a separate thread. Usually user interface operations should be done in .NET’s main (UI) thread.
There are several ways to use a separate (background) thread for your operations. For Windows Forms applications, simplest option is
BackgroundWorkercomponent.You can also create a
Threadobject yourself. But you should be careful about calling UI methods or changing UI controls properties in created thread. As I said, all the user interface operations (showing forms, changing controls texts or locations and so on) should be done in UI thread. Otherwise you get an exception. To do that, you can useControl.Invokemethod.