Exemplo n.º 1
0
        private void ThreadingTestButton_Click(object sender, RoutedEventArgs e)
        {
            Thread myThread = new Thread(() => {
                bool access = ThreadingTestButton.Dispatcher.CheckAccess();
                //the next line will fail with the exception:
                //System.InvalidOperationException: 'The calling thread cannot access this object because a different thread owns it.'
                //only the thread that created the object can
                //ThreadingTestButton.Content = "test";


                //an interesting note is that one could create a window, show it, and invoke the dispatcher to run
                //such that it tells the dispatcher of *this* thread to 'idle' (wait for UI input)
                //while it's technically multithreading, it doesn't allow parallelism within a window itself I.E:

                TestSubWindow tempWindow = new TestSubWindow();
                tempWindow.Show();
                System.Windows.Threading.Dispatcher.Run();

                //https://stackoverflow.com/a/1111485/3128017
                //https://stackoverflow.com/a/8669719/3128017
            });

            myThread.SetApartmentState(ApartmentState.STA);
            myThread.IsBackground = true;
            myThread.Start();
        }
Exemplo n.º 2
0
        private void DialogNotBlocksButton_Click(object sender, RoutedEventArgs e)
        {
            IsDialogReturnedButton.Text = "The dialog has not returned";
            TestSubWindow testSubWindow = new TestSubWindow();

            testSubWindow.Owner      = GetWindow(this);
            testSubWindow.Visibility = Visibility.Hidden;
            testSubWindow.Hide();
            testSubWindow.ShowDialog();
            IsDialogReturnedButton.Text = "The dialog has returned";
            testSubWindow.WindowState   = WindowState.Normal;
            testSubWindow.Show();
        }