0

I'm opening a second window in my main window. This window is used to display a progress bar, so I don't want to block my application when it is open.

My code is:

public partial class MainWindow : Window, IDisposable
{
    private void doUpdate(object sender, UpdateInfosArgs e)
    {
        this.Dispatcher.BeginInvoke(new Action(() =>
        {
            using (DownloadingFileWindow dlw = new DownloadingFileWindow())
            {
                dlw.OnDownloadFileComplete += OnDownloadFileComplete;
                this.Dispatcher.BeginInvoke((Action)(() => dlw.ShowDialog()));
                // or dlw.Show();
            }
        }
    }
}

Currently, when the window is shown, it is closed immediately when execution reaches the } of the using.

Is there a way to way that windows is closed?

René Vogt
  • 43,056
  • 14
  • 77
  • 99
A.Pissicat
  • 3,023
  • 4
  • 38
  • 93

2 Answers2

2

To open a dialog as non-modal (non-blocking) you need to use Show() instead of ShowDialog().

Your second problem is your using statement. This disposes dlw again at the end of the using block, which closes the window.

So try this:

private void doUpdate(object sender, UpdateInfosArgs e)
{
    DownloadingFileWindow dlw = new DownloadingFileWindow();
    dlw.OnDownloadFileComplete += OnDownloadFileComplete;
    dlw.Show();
}

I don't think it's necessary to call Dispose() on the child window explicitly. See this question for a discussion of that.

Community
  • 1
  • 1
René Vogt
  • 43,056
  • 14
  • 77
  • 99
1

The dialog will close after the using, because the using bracket cleans the object after finished the task in the using bracket.

To keep it open after your using bracket, you must not initiate the ressource for the using.

DownloadingFileWindow dlw = new DownloadingFileWindow()
dlw.OnDownloadFileComplete += OnDownloadFileComplete;
this.Dispatcher.BeginInvoke((Action)(() => dlw.ShowDialog()));

And within the Dialog itself, implement a closing routine like this.close(); by a trigger.

kurdy
  • 441
  • 4
  • 15