NotifyIcon fails to appear sometimes when my app starts at user login
My app:
About 5% of the time when the user logs into Windows the application launches as desired (I can see the app in the Task Manager) but the NotifyIcon fails to appear in the Windows Taskbar. No error seems to be thrown in the code, i.e. it seems as though .NET thinks the NotifyIcon was created successfully even though Windows is failing to show it. As a side note I have seen this same behavior happen with other apps that I have not written including the SysInterals Process Explorer app which is a Microsoft app.
What I think is happening:
It seems like if my app gets launched too quickly when the user is logging in then Windows is not "ready" to create NotifyIcons and so it doesn't show the NotifyIcon for my app.
Possible Solutions:
1) Delay the start of my app in the Task Scheduler to be 10 seconds (or 60 seconds, etc) after the user logs in. This works but I don't like this solutions because I have to replicate this setup to any computer I setup my app on including VMs and on my friends' computers.
2) Detect that the NotifyIcon failed to be displayed and try to remove and recreate it. I think this is the ideal solution. How can I detect this situation?
- .NET 7.0 Windows GUI app
- uses WPF for some parts of the GUI
- uses WinForms for other parts of the GUI
- uses the NotifyIcon class that is a part of WinForms (System.Windows.Forms.NotifyIcon) to create a NotifyIcon in the Windows Taskbar on the bottom right of the screen immediately next to the Windows clock.
- gets launched automatically when the user logs in (either via a shortcut in the "Start Menu\Programs\Startup" folder, a Task that launches it in the Task Scheduler, etc)
About 5% of the time when the user logs into Windows the application launches as desired (I can see the app in the Task Manager) but the NotifyIcon fails to appear in the Windows Taskbar. No error seems to be thrown in the code, i.e. it seems as though .NET thinks the NotifyIcon was created successfully even though Windows is failing to show it. As a side note I have seen this same behavior happen with other apps that I have not written including the SysInterals Process Explorer app which is a Microsoft app.
What I think is happening:
It seems like if my app gets launched too quickly when the user is logging in then Windows is not "ready" to create NotifyIcons and so it doesn't show the NotifyIcon for my app.
Possible Solutions:
1) Delay the start of my app in the Task Scheduler to be 10 seconds (or 60 seconds, etc) after the user logs in. This works but I don't like this solutions because I have to replicate this setup to any computer I setup my app on including VMs and on my friends' computers.
2) Detect that the NotifyIcon failed to be displayed and try to remove and recreate it. I think this is the ideal solution. How can I detect this situation?