c# – the calling thread must be STA, because ma…

While i have a WPF application which is original wpf and there is no much customization within, however, when I run this application, the application will fail with the information as follow.

“the calling thread must be STA, because many UI components require this”

STA thread?how comes other Wpf application does not have such an issue?

However, as the following reference has stated, it is because the Visual studio hosting process is enabled. ??how comes that way?

you can turn off this by

Project Properties > Debug > Enable Visual Studio hosting process

As in the second references, it is recommended to turn off this settings.
However, this raise a very interesting things, what if you want to run something which run in a STA aparment?

from this post from codeproject.

You might do this:

Thread thread = new Thread(
                () =>
                {
                  // ....
                }
                );


            // this has to be run from the STATthread apartment.

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();

References:
The calling thread must be STA, because many UI components require this
Enable or Disable Visual Studio Hosting Process?
The calling thread must be STA, because many UI components require this while navigation in wpf

Similar Posts: