0

I have a WPF dialog and I'm receiving the following error when I try to re-show the dialog after an exception is thrown...

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

I've done some reading around the problem and this message usually appears if you try to re-show a closed dialog, but I am not reusing the dialog...

Dialog dialog = new Dialog();
Exception exception = null;
IDisposable disposable = this.subject.Subscribe(
b =>
{
    dialog.DialogResult = b;
    dialog.Close();
},
ex =>
{
    dialog.Close();
    exception = new Exception("An unexpected error occured", ex);
});

bool? dialogResult = dialog.ShowDialog();
disposable.Dispose();
if (exception != null)
{
   throw exception;
}

I can execute this code as many times as I want until the exception is thrown which is when the message above is displayed on show dialog. The code is executing on the main UI thread.

Does anyone have any idea why throwing the exception is stopping a new dialog from being shown?

Regards, Jason

3 Answers 3

0

You shouldn't be calling Dispose() yourself, this can cause a lot of problems with partially disposed objects. Use a using block instead.

An exception being thrown is most likely causing a only partial dispose of the dialog object resulting in your error.

Is Dialog a custom class? I don't recognize it as being part of WPF. If so, make sure you implement IDisposable properly (according to MSDN).

0

Without knowing what subject.Subscribe does, it is a little difficult to know for sure, but I think this is what is happening.

The Subscribe method doesn't return immediately while everything is fine, so your success delegate isn't called until after you call ShowDialog.

However, it seems to be calling the failure delegate immediately upon Subscribe after getting one exception. Then, when you call ShowDialog, your Dialog's Close has already been called, and therefore will throw the exception you are talking about.

You can see the same behavior if you were to do the following:

dialog.Close()
dialog.ShowDialog()

Again, without knowing the details of the subject class, I can't really offer a way of solving your particular issue, but hopefully you can track down your issue from here.

-1

Thanks for the replies. I've managed to solve the problem but I'm not 100% sure why this solves the problem. Here's what the code looks like now...

Dialog dialog = new Dialog();
Exception exception = null;
this.subject = new Subject<bool>();
IDisposable disposable = this.subject
    .ObserveOnDispatcher()
    .Subscribe(
         b =>
         {
              dialog.DialogResult = b;
              dialog.Close();
         },
         ex =>
         {
              dialog.Close();
              exception = new Exception("An unexpected error occurred.", ex);
         });

 bool? dialogResult = dialog.ShowDialog();
 disposable.Dispose();
 if (exception != null)
 {
     throw exception;
 }

The key was to use ObserveOnDispatcher(), I thought that maybe the error handler was calling dialog.Close() on a background thread which was causing the strange beahviour, but regardless of whether I use ObserveOnDispatcher(), the OnError handler still gets fired on the main UI thread, weird!

I also needed to new the subject each time as once OnError has been called, anything that subscribes to the subject will immediately fire OnError! An alternative approach to disposing the subscription manually could be to call subject.OnCompleted in the OnNext handler but I think it's clearer to call dispose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.