(Lviv community of .NET developers)

Windows&Threads

August 13, 2007 20:00 by rat

Hi,

 

Recently I have been reading about the threading support in WPF, and it is mostly the same as in WinForms. 

But some things are different, for example here is how we make second window running in it's own thread: 

 

 private void NewWindowHandler( object sender, RoutedEventArgs e )

{

 Thread newWindowThread = new Thread( new ThreadStart( ThreadStartingPoint ) );

 newWindowThread.SetApartmentState( ApartmentState.STA );

 newWindowThread.IsBackground = true;

 newWindowThread.Start();

}

 

private void ThreadStartingPoint()

{

 Window1 tempWindow = new Window1();

 tempWindow.Show();

 System.Windows.Threading.Dispatcher.Run();

}

 

So the trick is that we create the window in a separate thread, window's Dispatcher is attached to the thread it is created on, so the window is not attached to the main GUI thread.

Than we just  start the processing of message queue by calling Dispatcher.Run()

The original source of this piece of wisdom is here: 

http://msdn2.microsoft.com/en-us/library/ms741870.aspx 

 


Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: ,
Categories: WinFx
Actions: Permalink | Comments (0) | RSSRSS comment feed

Add comment


(Will show your Gravatar icon)