Thursday, March 10, 2016

Running UI on difference dispatcher thread

It may be beneficial to run separate UI event loop, though performance testing is warranted.

        private void button_Click(object sender, RoutedEventArgs e)
        {
            this.Title = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();
            Thread th = new Thread(new ThreadStart(show));
            th.SetApartmentState(ApartmentState.STA);
            th.IsBackground = false;  // true=> just a child window
            th.Start();
        }
        void show()
        {

            Window1 w = new Window1() { Title = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() };
            w.Show();
            w.Closed += (sender2, e2) =>
                w.Dispatcher.InvokeShutdown();

            System.Windows.Threading.Dispatcher.Run(); //must run to start event-loop 
        }

No comments:

Post a Comment