Quantcast
Viewing all articles
Browse latest Browse all 18858

Access the dispatcher of another Thread.

Hello guys.

I the project i'm developing there was a need to create a new tread with a window with her own dispatcher, so it can run freely from the main dispatcher.

I did something like this: 

        //button click from main ui thread
        private void btn_Click(object sender, RoutedEventArgs e)
        {
            // Set the apartment state
            newWindowThread.SetApartmentState(ApartmentState.STA);
            // Make the thread a background thread
            newWindowThread.IsBackground = true;
            // Start the thread
            newWindowThread.Start();
         }

        // Create a thread
        Thread newWindowThread = new Thread(new ThreadStart(() =>
        {
            // Create and show the Window
            MyWindow tempWindow = new MyWindow ();
            tempWindow.Show();
            tempWindow.Closed += (sender2, e2) => tempWindow.Dispatcher.InvokeShutdown();

            // Start the Dispatcher Processing
            System.Windows.Threading.Dispatcher.Run();
        }));

It works very nice but now....

  how can i call a method of tempWindow from the second thread?

I saw something like:

System.Windows.Threading.Dispatcher.FromThread(newWindowThread).Invoke(new Action(() =>
{
     //call stuff here????          
}), DispatcherPriority.ContextIdle, null);

how can i call the method. I'm confused!

Thanks!


Viewing all articles
Browse latest Browse all 18858

Trending Articles