hardcodet / wpf-notifyicon

NotifyIcon (aka system tray icon or taskbar icon) for the WPF platform
Other
810 stars 125 forks source link

`TaskbarIcon.Dispose` throws exception if not called from application's main thread #92

Open jhinder opened 1 year ago

jhinder commented 1 year ago

The Dispose method of TaskbarIcon is not thread-safe, it can only be called from the WPF application's main thread.

This should either be documented, or the implementation should be made safe e.g. by making use of the Application instance's Dispatcher.

Version: 1.1.0

Steps to reproduce:

Result

System.InvalidOperationException: "The calling thread cannot access this object because a different thread owns it."

Callstack:

    WindowsBase.dll!System.Windows.Threading.Dispatcher.VerifyAccess()  Unknown
    WindowsBase.dll!System.Windows.Threading.DispatcherObject.VerifyAccess()    Unknown
    PresentationFramework.dll!System.Windows.Application.Exit.remove(System.Windows.ExitEventHandler value) Unknown
    Hardcodet.NotifyIcon.Wpf.dll!Hardcodet.Wpf.TaskbarNotification.TaskbarIcon.Dispose(bool disposing)  Unknown
    Hardcodet.NotifyIcon.Wpf.dll!Hardcodet.Wpf.TaskbarNotification.TaskbarIcon.Dispose()    Unknown
>   TrayIconThreadsafety.dll!TrayIconThreadsafety.MainWindow.Button_Click.AnonymousMethod__0() Line 36  C#
    System.Private.CoreLib.dll!System.Threading.Thread.StartHelper.Callback(object state)   Unknown
    System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)   Unknown
    System.Private.CoreLib.dll!System.Threading.Thread.StartCallback()  Unknown