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

bugfix - hotkey output erase #826

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions HandheldCompanion/Managers/Hotkeys/Hotkey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,21 +347,25 @@ public void DrawInput()

private void DrawOutput()
{
// update button content
switch (HasOutput())
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
{
case true:
mainControl.HotkeyOutput.Content =
string.Join(", ", inputsChord.OutputKeys.Where(key => key.IsKeyDown));
mainControl.HotkeyOutput.SetResourceReference(Control.ForegroundProperty,
"SystemControlForegroundBaseHighBrush");
break;
case false:
mainControl.HotkeyOutput.Content = Resources.ResourceManager.GetString("InputsHotkey_fallbackOutput");
mainControl.HotkeyOutput.SetResourceReference(Control.ForegroundProperty,
"SystemControlForegroundBaseMediumBrush");
break;
}
// update button content
switch (HasOutput())
{
case true:
mainControl.HotkeyOutput.Content =
string.Join(", ", inputsChord.OutputKeys.Where(key => key.IsKeyDown));
mainControl.HotkeyOutput.SetResourceReference(Control.ForegroundProperty,
"SystemControlForegroundBaseHighBrush");
break;
case false:
mainControl.HotkeyOutput.Content = Resources.ResourceManager.GetString("InputsHotkey_fallbackOutput");
mainControl.HotkeyOutput.SetResourceReference(Control.ForegroundProperty,
"SystemControlForegroundBaseMediumBrush");
break;
}
});

DrawErase();
}
Comment on lines 347 to 371
Copy link
Contributor

Choose a reason for hiding this comment

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

The changes correctly move the UI update logic into a dispatcher call to ensure that the UI elements are accessed on the main UI thread. This is a common and necessary practice in WPF applications to avoid cross-thread operation errors. The logic within the dispatcher call remains the same, which is good as it preserves the existing functionality while fixing the thread safety issue.

However, it's important to ensure that Application.Current and Application.Current.Dispatcher are not null before invoking methods on them. This can happen in scenarios where the application is shutting down or if the code is running in a non-UI thread without a synchronization context.

To safeguard against potential null reference exceptions, you could add a null check before the dispatcher invoke:

if (Application.Current?.Dispatcher != null)
{
    Application.Current.Dispatcher.BeginInvoke(() =>
    {
        // existing code
    });
}

This is a minor change but can prevent runtime errors in edge cases.

Expand Down
Loading