Where is the right place to start background threads in C # WinForms applications?

I want to start 2 background threads. One thread acts as a server for the client, and another thread acts as a client for the server. This application is both a server and a client application. Should I run these threads in the Main () function before calling Application.Run ()? Or should they be created after loading the main form? I am trying to isolate the interface and business logic from each other, but I know that these threads will need to notify form events. What is the best coding practice for this? Thank!

+3
source share
2 answers

If they need to execute logic in the main form, you will need this logic to run in the stream on which the main form was created. Thus, both the client and the server will need to have a link to the main form so that they can invoke Invoke or BeginInvoke on it (depending on whether the code in the main form should be synchronous or asynchronous). Therefore, you should probably start streams after loading the main form, so that you can pass a link to it to the client and server.

+1
source

Run them in Main()before creating the user interface.

0
source

All Articles