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(); }
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(); }
private void MemoryLeakTestingButton_Click(object sender, RoutedEventArgs e) { //GC.Collect(); //GC.WaitForPendingFinalizers(); //GC.Collect(); window = new TestSubWindow(); window.ShowDialog(); //don't need to call Close() to free up memory. It won't. // only setting it to null will allow it to be collected //window.Close(); window = null; //GC.Collect(); //GC.WaitForPendingFinalizers(); //GC.Collect(); //in this current configuration, the TestSubWindow does *not* show up in the memory profiler }