Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- private Window _Window;
- static void Main(string[] args)
- {
- Program p = new Program();
- p.WPFCommand();
- }
- public void WPFCommand()
- {
- if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
- {
- Thread thread = new Thread(() =>
- {
- System.Threading.ApartmentState aptState = Thread.CurrentThread.GetApartmentState();
- Console.WriteLine("Spawned thread apartment: {0}", aptState); // <- Still MTA
- // !!! If _Window is the member of the class, the thread will be MTA
- // !!! otherwise STA
- _Window = new Window();
- System.Windows.Threading.Dispatcher.Run();
- });
- Console.WriteLine("Thread apartment state1: {0}", thread.GetApartmentState());
- thread.SetApartmentState(ApartmentState.STA); // <- even though set as STA
- Console.WriteLine("Thread apartment state2: {0}", thread.GetApartmentState());
- thread.IsBackground = true;
- thread.Start();
- thread.Join();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement