Are there any problems with having a group of winning forms in one thread and another group in another thread?

I have an application that has two different groups of winning forms, and I want each group to work in separate threads. Are there any problems with this approach as long as I run Invoke / Invoke when operations happen on different threads?

This question is related to the fact that I am always used to thinking in terms of the “gui stream”, which should if (InvokeRequired) { Invoke } else { ... } , and all forms live in this stream.

An alternative angle on this:

Is there anything special about the default stream in which there are winnings, or is it the same as any other stream?

+4
source share
3 answers

Well, there are ways to take your foot off, but Windows Forms rarely forgets to tell you about it.

Yes, there is something special about the “main theme”. He works in STA mode, a single apartment. This is a mode that affects COM components, shell dialogs such as OpenFileDialog, and operations such as Drag + Drop and Clipboard. Themes that display the user interface should always be STAs. This is automatic in normal WF applications with the [STAThread] attribute in the Main () method. In your application, you must call Thread.SetApartmentState () before starting it. And the stream is special because it pumps up the message pipeline (Application.Run), a requirement for STA streams.

By default, any thread that you start is launched, or the threadpool thread runs in MTA mode. Threadpool threads cannot be changed, they are always MTA.

+2
source

It should work fine, I'm sure that in my current project this is implemented in this way, and we have not seen any problems with this. You just need to remember to use the correct control when using the InvokeRequired and Invoke methods.

The GUI thread just passes messages so that it can handle standard Windows messages, I don’t think there is anything special about this.

0
source

The only problem I can think of is with very old COM components that are connected to the main single-threaded apartment.

msdn.microsoft.com

but it is very unlikely

0
source

Source: https://habr.com/ru/post/1299729/


All Articles