Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to create a TrayIcon: TryCreate failed. #103

Open
diazynez opened this issue Sep 1, 2023 · 10 comments
Open

Unable to create a TrayIcon: TryCreate failed. #103

diazynez opened this issue Sep 1, 2023 · 10 comments
Labels
bug Something isn't working

Comments

@diazynez
Copy link

diazynez commented Sep 1, 2023

Describe the bug

In Scighost/Starward using the H.NotifyIcon project, the TrayIcon cannot be generated.
Starward's author stated that the problem came from an internal error by H.NotifyIcon, leaving him unable to determine the specific cause.
Link here > Scighost/Starward#251
The log is as follows.

Steps to reproduce the bug

No response

Expected behavior

No response

Screenshots

[20:56:32.288] [EROR] Starward.Services.SystemTrayService
Initialize system tray
System.InvalidOperationException: TryCreate failed.
at H.NotifyIcon.Core.TrayIcon.Create() in //src/libs/H.NotifyIcon/Core/TrayIcon.cs:line 324
at H.NotifyIcon.TaskbarIcon.ForceCreate(Boolean ) in //src/libs/H.NotifyIcon.Shared/TaskbarIcon.cs:line 134
at Starward.Services.SystemTrayService.Initialize(GameBiz gameBiz) in D:\a\Starward\Starward\src\Starward\Services\SystemTrayService.cs:line 72

NuGet package version

No response

Platform

WinUI

IDE

Visual Studio 2022

Windows Version

Windows 10

WindowsAppSDK Version

1.1, Other

WindowsAppSDK Type

Packaged

Manifest

No response

Additional context

No response

@diazynez diazynez added the bug Something isn't working label Sep 1, 2023
@HavenDV
Copy link
Owner

HavenDV commented Sep 1, 2023

Unfortunately, this error simply indicates that the Windows API request failed and it does not provide any information about why this happened.
I'd be happy to hear about more specific details, but I don't have time to do it myself yet

@diazynez
Copy link
Author

diazynez commented Sep 1, 2023

That's a shame :( , but I don't know how to get a more detailed error log, I'm just an average and ignorant user of C#.

@HavenDV
Copy link
Owner

HavenDV commented Sep 1, 2023

The fact of the matter is that it doesn't exist. The lowest level does not provide any information. We can only try something and select the conditions for the error to occur, and try to understand the problem based on the conditions

@mlrsyz
Copy link

mlrsyz commented Sep 5, 2023

Some netizens returned to normal after executing the following script to clean thumbnails. The main ones should be the two lines in the registry.

taskkill /f /im explorer.exe
del /f /s /q /a "%LocalAppData%\IconCache.db"
cd /d "%LocalAppData%\Microsoft\Windows\Explorer"
del /f /s /q /a thumbcache_.db
del /f /s /q /a iconcache_
.db
reg delete "HKCR\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v IconStreams /f
reg delete "HKCR\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v PastIconsStream /f
start explorer

@diazynez
Copy link
Author

diazynez commented Sep 5, 2023

Some netizens returned to normal after executing the following script to clean thumbnails. The main ones should be the two lines in the registry.

taskkill /f /im explorer.exe del /f /s /q /a "%LocalAppData%\IconCache.db" cd /d "%LocalAppData%\Microsoft\Windows\Explorer" del /f /s /q /a thumbcache__.db del /f /s /q /a iconcache__.db reg delete "HKCR\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v IconStreams /f reg delete "HKCR\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v PastIconsStream /f start explorer

I personally tried to find this method is indeed feasible, after running the script has successfully solved the problem, thank you for your generous answer!

@HavenDV
Copy link
Owner

HavenDV commented Sep 5, 2023

Unfortunately, registry manipulation requires administrator rights, so this is not applicable in the library. I'll take a deeper look when I have some free time.

@Khiro95
Copy link
Contributor

Khiro95 commented Apr 18, 2024

I've faced this issue today while using TrayIconWithContextMenu with constant name and the solution, in my case, was to simply change the name of the tray icon to make it generate different GUID. It turned-out that the same GUID should be used from the same path, and that was the issue in my case because the Debug version worked fine but the Release version was crashing.

From NotificationIcon Sample:

Notification icons specified with a GUID are protected against spoofing by validating that only a single application registers them. This registration is performed the first time you call Shell_NotifyIcon(NIM_ADD, ...) and the full path name of the calling application is stored. If you later move your binary file to a different location, the system will not allow the icon to be added again. Please see Shell_NotifyIcon for more information.

@Khiro95
Copy link
Contributor

Khiro95 commented Apr 18, 2024

@HavenDV So maybe using the parameterless constructor of TrayIcon & TrayIconWithContextMenu should be the recommended way?

@HavenDV
Copy link
Owner

HavenDV commented Apr 19, 2024

@HavenDV So maybe using the parameterless constructor of TrayIcon & TrayIconWithContextMenu should be the recommended way?

Yes, I use it also in TaskbarIcon class of main lib.

@BaY1251
Copy link

BaY1251 commented Dec 2, 2024

WPF platform has similar issues,Occasionally occurs

System.InvalidOperationException: TryCreate failed. at H.NotifyIcon.Core.TrayIcon.Create() in /_/src/libs/H.NotifyIcon/Core/TrayIcon.cs:line 332 at H.NotifyIcon.TaskbarIcon.ForceCreate(Boolean enablesEfficiencyMode) in /_/src/libs/H.NotifyIcon.Shared/TaskbarIcon.cs:line 128 at VMS.View.MainWindow.<.ctor>b__2_0(Object <p0>, EventArgs <p1>) at System.Windows.Window.WmSizeChanged(IntPtr wParam) at System.Windows.Window.WindowFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants