user3060586
user3060586

Reputation: 133

Weird behaviour of Window.ShowDialog

I just got a weird behavior in my WPF application, hope somebody will give me a clue. I have the following overriden Application's OnStartup:

protected override void  OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        try
        {
            if (AppMutex.WaitOne(TimeSpan.Zero, true))
            {
                IoC.Instance.RegisterInstance(LogManager.GetLogger("MainLogger"));
                Init();

                var mainView = new MainView()
                {
                    DataContext = IoC.Instance.MainViewModel                                            
                };

                mainView.ShowDialog();                  
            }
        }
        catch (Exception exception)
        {
            IoC.Instance.Log.Error(exception);
            throw;
        }
        finally
        {
            AppMutex.ReleaseMutex();
        }

    }

Here MainView is a subclass of System.Windows.Window and has no overriden methods. The trouble is with mainView.ShowDialog(). This line is executing without any exceptions but the window isn't shown. Thread just goes forward and application terminates. Has somebody any ideas about this behavior? Thanks in advance!

UPD: If I place MessageBox.ShowDialog() before this call, then MessageBox.ShowDialog executing correct, but mainView.ShowDialog throws an exception:

Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.

at System.Windows.Window.VerifyCanShow()
at System.Windows.Window.ShowDialog() 

with null InnerException.

UPD: I got symbols and checked VerifyCanShow:

private void VerifyCanShow()
{
  if (this._disposed)
    throw new InvalidOperationException(SR.Get("ReshowNotAllowed"));
}

Looks like my mainView getting disposed in some way...

Upvotes: 2

Views: 932

Answers (1)

Ming Slogar
Ming Slogar

Reputation: 2357

You need to set Application.ShutdownMode to OnExplicitShutdown. http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode(v=vs.110).aspx

You can then manually shut down the application after your dialog closes.

mainView.ShowDialog();
Shutdown();

Upvotes: 1

Related Questions