How to show a status dialog while parent form is closing
I have a WPF app that takes a while to close so I want to add a 'please wait' dialog to show up while the main form is closing. However, when I added 'formmessage.show' to my main form's closing event handler, the form displays but instead of the 'please wait' text, all i get is a white rectangle. This only seems to happen when calling this code from 开发者_JAVA百科the closing handler. It works fine from other handlers (form click or maximize). Can anyone help? Here is my simplified code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Form1 f = new Form1();
f.Show();
System.Threading.Thread.Sleep(3000);
}
}
Form1 has a label on it which says 'please wait'.
Try out:
Application.Current.Dispatcher.Invoke(
DispatcherPriority.Normal,
new Action(() =>
{
Form1 f = new Form1();
f.Show();
System.Threading.Thread.Sleep(3000);
}));
BTW, you can remove Thread.Sleep()
because default value of the Application Shutdown mode is OnLastWindowClose so Application will be active until an user explicitly close active window, this behaviour could be changed by switching Application.ShutdownMode
to OnMainWindowClose
in the App.xaml
file)
That would be because of the fact your creating the form in the same thread your telling to sleep, during which it's not going to respond to draw requests, try launching the form in another thread, and closing it in MainWindow's destructor.
精彩评论