Advertisement
Guest User

Untitled

a guest
Jun 15th, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1.     class Program
  2.     {
  3.         private Window _Window;
  4.  
  5.         static void Main(string[] args)
  6.         {
  7.             Program p = new Program();
  8.             p.WPFCommand();
  9.         }
  10.  
  11.         public void WPFCommand()
  12.         {
  13.             if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
  14.             {
  15.                 Thread thread = new Thread(() =>
  16.                 {
  17.                     System.Threading.ApartmentState aptState = Thread.CurrentThread.GetApartmentState();
  18.                     Console.WriteLine("Spawned thread apartment: {0}", aptState); // <- Still MTA
  19.  
  20.                     // !!! If _Window is the member of the class, the thread will be MTA
  21.                     // !!! otherwise STA
  22.                     _Window = new Window();
  23.  
  24.                     System.Windows.Threading.Dispatcher.Run();
  25.                 });
  26.  
  27.                 Console.WriteLine("Thread apartment state1: {0}", thread.GetApartmentState());
  28.                 thread.SetApartmentState(ApartmentState.STA);     // <- even though set as STA
  29.                 Console.WriteLine("Thread apartment state2: {0}", thread.GetApartmentState());
  30.  
  31.                 thread.IsBackground = true;
  32.                 thread.Start();
  33.                 thread.Join();
  34.             }
  35.         }
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement