0

I developed a WPF Application where I have problems when closing the application.

Only on Windows 2003 PCs, the application throws the following exception on closing. But it does not seem to be thrown from my code, because I can't get a callstack.

That's why I can't post any further details.

Do you have a clue where I can start digging into it?

System.InvalidOperationException was unhandled
  Message=Handle is not initialized.
  Source=mscorlib
  StackTrace:
       at System.WeakReference.set_Target(Object value)
       at System.Windows.Threading.Dispatcher.FromThread(Thread thread)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.IntDestroyWindow(HandleRef hWnd)
       at MS.Win32.HwndWrapper.DestroyWindow(Object args)
       at MS.Win32.HwndWrapper.Dispose(Boolean disposing, Boolean isHwndBeingDestroyed)
       at MS.Win32.HwndWrapper.Finalize()
  InnerException: 

Thanks for your ideas.

EDIT I found out which lines of code produces the failure. But how can I fix it?

It's the following line of code:

        try
        {
            return DesignerProperties.GetIsInDesignMode(new DependencyObject());
        }
        catch (Exception)
        {
            return true;
        }

I'm using this to check if the code runs in the designer. But on closing this code fails, although I catched the exception.

Any other ideas to check the designmode? Thanks for your help.

BitKFu
  • 3,649
  • 3
  • 28
  • 43
  • Well mscorlib is the .net framework core library so looks like you got an issue in there - weak reference looks to be the culprit.. but why is anyones guess :). You tried creating a simple WPF app with no code and closing that too see if you get the same issue? (Of course on the Win2003 machines) – Charleh Aug 07 '12 at 13:13
  • Please see my edit. I found the bug, but don't know a workaround ... – BitKFu Aug 07 '12 at 13:24
  • before you ask: I'm using new DependencyObject(), because I want to check this in my ViewModel – BitKFu Aug 07 '12 at 13:27
  • Wonder if it's anything to do with creating a new object while the thread is terminating... where is this code located? – Charleh Aug 07 '12 at 13:28
  • Ok - have you tried passing a reference to the view instead of using new DepObj? – Charleh Aug 07 '12 at 13:29
  • I think you're not supposed to just new up a DependencyObject and use that in the GetIsInDesignMode() call. Maybe try calling GetIsInDesignMode() on a proper element? – TheNameless Aug 07 '12 at 13:29
  • I'm checking this the time the ViewModel is going to be disposed ... Hm. Thinking about why I have implemented it that way. Think it's because I'm doing some de-registration stuff, which is not needed in design mode. – BitKFu Aug 07 '12 at 13:45

1 Answers1

0

To answer my own question ...

I could solve the issue by implementing a backing field for the IsInDesignMode Property. The backing field is now going to bet set within the constructor of the ViewModel, instead of my prior solution which checked the DesignMode on every method call.

That means, that even within the Dispose Method it can be checked if the application is/was running in DesignMode.

BitKFu
  • 3,649
  • 3
  • 28
  • 43