NotifyIcon fails to appear sometimes when my app starts at user login

My app: - .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) Problem: 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?
1 Reply
PurplePeopleEater
PurplePeopleEaterOP8mo ago
3) Use my application code to delay showing the NotifyIcon if my app is being launched as part of the user logging in. I think this is an acceptable solution, but not ideal because I have to make an assumption about how long of a delay after logging in will make creating the NotifyIcon succeed. Approximate pseudo-code: if not Debugger.IsAttached then if CurrentTime - CurrentUser.LoginTime < 10 seconds then start a timer that creates the NotifyIcon after the user has been logged in for 10 seconds else // 10 seconds or more has already elapsed since the user logged in so create NotifyIcon without delaying create NotifyIcon end if else // We are running via VS debugger so create NotifyIcon without delaying create NotifyIcon end if Question: - Does anyone know how to pull off solution #2 above as I think that would be the ideal solution? - Does anyone have any other solutions that they have experience with that work really reliably? Thank you in advance for any help!
Want results from more Discord servers?
Add your server