question

leo215 avatar image
0 Votes"
leo215 asked leo215 commented

Modeless WPF dialog not rendering control content.

I have a WPF app that upon startup, it shows a modeless dialog while it connects to a WCF service and retrieves some data. Once data is received, the modeless dialog is closed. The modeless dialog during startup is displayed correctly.

The data retrieved from the WCF service is displayed in a ListView. When an item is double-clicked, the same modeless dialog is displayed (with different title and message) until the item's properties are retrieved from the WCF service. When the properties are retrieved, the modeless dialog is closed, and a new dialog is displayed showing the item's properties. It is during this process of retrieving the item's properties that the modeless dialog does not render correctly. The modeless dialog contains a textbox and button. The textbox shows a message to the user and the button allows the user to cancel the operation. The dialog is shown with the correct title but the message text and cancel button are not shown. This is the same dialog that displayed correctly during startup.

My understanding of a modeless dialog is that when the Show() method is called on it, it returns immediately, and the code continues executing. The only thing I can think of is that the app is too busy doing other stuff while it waits for the item properties to be returned and this does not allow the modeless dialog to finish rendering.

The dialog is defined in a general-purpose library. It is very basic containing only a textbox and button and public properties that set their values.

Why would the modeless dialog not render correctly?

Any help would be greatly appreciated.

Thanks.
LA

dotnet-wpf-xaml
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@leo215 . Welcome Microsoft Q&A.
Could you show me the complete sample code that reproduces your problem to analyze?

0 Votes 0 ·
       public partial class MainWindow : Window
     {
         private ManualResetEvent hEvent = new ManualResetEvent(false);
         Task connectionStatusThread;
         CancellationTokenSource cts;
   
         public MainWindow()
         {
             InitializeComponent();
         }
    
         private void Window_Loaded(object sender, RoutedEventArgs e)
         {
             WaitDlg dlg = new WaitDlg();
             dlg.Owner = this;
             dlg.Title = "Loading";
             dlg.Message = "Loading...";
             dlg.Show();
    
             SimulateWork();    
             dlg.Close();
         }
    
         private void SimulateWork()
         {
             cts = new CancellationTokenSource();
             connectionStatusThread = Task.Factory.StartNew(() => ConnectionStatusThread(cts.Token), cts.Token);
             Task.Delay(3000).Wait();
             return;
         }
         private void ShutDown()
         {   
             cts.Cancel();
             connectionStatusThread.Wait();
         }
                    
         private void ConnectionStatusThread(CancellationToken token)
         {
             while(token.IsCancellationRequested==false)
             {
                 Task.Delay(1000).Wait();
                 Task.Yield();
             }
    
             Task.Delay(3000).Wait();
         }
                    
         private void Close_Clicked(object sender, RoutedEventArgs e)
         {
             WaitDlg dlg = new WaitDlg();
             dlg.Owner = this;
             dlg.Title = "Shutting Down";
             dlg.Message = "ShutDown...";
             dlg.Show();
    
             ShutDown();
    
             dlg.Close();
             Close();
         }    
     }   

0 Votes 0 ·

1 Answer

SimpleSamples avatar image
0 Votes"
SimpleSamples answered

Yes, the problem very well can be that the app is busy doing other stuff and does not allow the modeless dialog to thoroughly render. You can use a BackgroundWorker to do the other processing. There are other ways to do it and many articles. The important thing is that you need to create another task for doing other processing. The main thread is the one that creates the windows. You need to avoid keeping it busy.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.