Search code examples
c#wpfmessagebox

Removal of MessageBox Causes WPF Application to Crash on setting Content


I am trying Change the Content of ContentControl when a DataGrid is double clicked with the Following Code:

    private void homeGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        if (homeGrid.SelectedItem != null)
        {                
            Document selectedDoc = (Document)homeGrid.SelectedItem;
          
            // Will Crash if this Message Box Removed
                MessageBox.Show(selectedDoc.FilePath);     

            mainWindow.contentControl.Content = new DocumentView(selectedDoc);
        }           
    }

Which Works ok and the content is loaded, however if I remove the message box i recieve the following error:

An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll
Additional information: Dispatcher processing has been suspended, but messages are still being processed.

How can I remove this message box and still have the content load properly ?

Thank You.


Solution

  • I removed the Event From Double Click on the Grid and placed it under a button as follows:

       private void Go_Click(object sender, RoutedEventArgs e)
        {
            if (homeGrid.SelectedItem != null)
            {
                Document selectedDoc = (Document)homeGrid.SelectedItem;
    
                // Will Crash if this Message Box Added?
                //     MessageBox.Show(selectedDoc.FilePath);                            
    
                mainWindow.contentControl.Content = new DocumentView(selectedDoc);
            }           
        }
    

    This has resolved the issue, though confusingly now Adding the message box back in causes the same exception. perhaps related to this question : WPF : Dispatcher processing has been suspended, but messages are still being processed