Home > Software engineering >  Are there consequences to putting Application.Run() in a different Thread than the initial one?
Are there consequences to putting Application.Run() in a different Thread than the initial one?

Time:10-01

I am currently trying my hand at .NET generic host stuff in combination with WinForms and am experimenting with something like this:

var mainmenu = new Thread(() => Application.Run(_mainMenuView as Form));
mainmenu.SetApartmentState(ApartmentState.STA);
mainmenu.Start();

with _mainMenuView being an interface implemented by the MainMenuView Form which I get from DI yadayadayada.

My questions now are:

  • Does this have unforeseen consequences?
  • Do I need to join the Thread back to my "main" Thread?
  • Does the Thread die if I just close the Form?
  • How safe is it to get or set properties via the interface? Do I need to care about Invocation?

Thanks for helping

CodePudding user response:

I've been expirimenting with this also. You can run the Application.Run on another thread,

  • Do I need to join the Thread back to my "main" Thread?
  • How safe is it to get or set properties via the interface? Do I need to care about Invocation?

If you want to set any properties from a thread which is not the same as the application.run runs on. you should care about locking.

  • Does the Thread die if I just close the Form?

The thread will finish when you close the form.

  • Does this have unforeseen consequences?

One problem you will be facing is, the Application class uses static properties/methods. So you could get strange behavior when trying to create multiple threads with application.run on it.

  • Related