Search code examples
c#wpfnotifyicon.net-4.5.2

WPF NotifyIcon Crash On First Run - The root Visual of a VisualTarget cannot have a parent


Update: Issue appears to be introduced with .NET 4.5.2. Problem does not occur with either 4.5.1 or 4.5.

I have a strange issue I am having difficulty debugging. We have a WPF application built on top of the NotifyIcon made by Philipp Sumi. http://www.codeproject.com/Articles/36468/WPF-NotifyIcon http://www.hardcodet.net/wpf-notifyicon

We are using the verion from nugget:

<package id="Hardcodet.NotifyIcon.Wpf" version="1.0.5" targetFramework="net45" />

The problem is that the first time (and only the first time) the application is ran, it fails with the following exception. It is easy to recreate by hovering the mouse over the system tray icon when it appears. On subsequent runs there is no problem. The application does not have any saved state or persistent data. I'm not aware of any difference between the first and subsequent runs. It does however start much faster the second time. The same problem occurs in the windowless example application that comes with NotifyIcon.

Exception Message and Stack Trace


Solution

  • I have not been able to identify what the real cause of the problem is. It seems to be changes made in the 4.5.2 version of the framework. I did find the following work around.

    At the beginning of the application simply create a tool tip and display it. This seems to cause things to get constructed correctly. For example:

    [STAThread]
    static void Main()
    {
        ToolTip tt = new ToolTip();
        tt.IsOpen = true;
        tt.IsOpen = false;
    ...
    }
    

    I would still like to know what the real issue is, so if anyone knows please post.