Skip to content

Fix tray hook failure by marshalling updates to UI thread - #132

Merged
maraf merged 1 commit into
mainfrom
maraf-fix-hook-failed-ui-thread
Jul 24, 2026
Merged

maraf merged 1 commit into
mainfrom
maraf-fix-hook-failed-ui-thread

Conversation

@maraf

@maraf maraf commented Jul 24, 2026

Copy link
Copy Markdown
Member

Why

When the global hotkey hook fails, the failure callback can run on a background thread. Updating Avalonia tray properties from that thread throws InvalidOperationException: Call from invalid thread.

What changed

TrayIcon.OnHookFailed now marshals tray UI updates onto Dispatcher.UIThread.

  • Added Avalonia.Threading import.
  • Wrapped tooltip and menu header updates in a local UpdateUi action.
  • If already on the UI thread, run directly; otherwise post to the UI dispatcher.

Notes

This keeps the existing behavior and message text, but makes the failure path thread-safe.

Marshal TrayIcon UI updates in OnHookFailed onto Dispatcher.UIThread to avoid Avalonia invalid thread exceptions when the hotkey hook faults on a background thread.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: b4eea782-178d-4d75-9182-24b22f627186

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes the tray icon’s hotkey-failure UI updates thread-safe by ensuring tooltip/menu mutations are executed on Avalonia’s UI thread, preventing InvalidOperationException when the global hook fails on a background thread.

Changes:

  • Added Avalonia.Threading usage to marshal tray UI updates to Dispatcher.UIThread.
  • Encapsulated tooltip and menu header mutations into a local UpdateUi action.
  • Executes updates synchronously when already on the UI thread; otherwise posts to the UI dispatcher.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@maraf
maraf merged commit 36b63d3 into main Jul 24, 2026
2 checks passed
@maraf
maraf deleted the maraf-fix-hook-failed-ui-thread branch July 24, 2026 08:36
@maraf maraf added bug Something isn't working app-mac labels Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app-mac bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants