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

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

Closed
jhinder opened this issue Jan 26, 2023 · 0 comments · Fixed by #93
Closed

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

jhinder opened this issue Jan 26, 2023 · 0 comments · Fixed by #93
Labels
Bug Bug or Bug fixes
Milestone

Comments

@jhinder
Copy link

jhinder commented Jan 26, 2023

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:

  • Create a WPF Application project.
  • Add a button with a Clicked handler to the main window.
  • Add this code to the handler:
    var taskbarIcon = new TaskbarIcon();
    var t = new Thread(() =>
    {
        taskbarIcon.Dispose();
    });
    t.Start();
  • Run the app and click the button

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

jhinder pushed a commit to jhinder/wpf-notifyicon that referenced this issue Jan 30, 2023
@punker76 punker76 added the Bug Bug or Bug fixes label Aug 5, 2024
@punker76 punker76 added this to the 2.0.0 milestone Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug or Bug fixes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants