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

Fix WPF crash due to system theme watcher related COMExceptions #796

Merged
merged 4 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion TwitchDownloaderWPF/PageQueue.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<TextBlock Text="{Binding Status, StringFormat=Status: {0}, Mode=OneWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" Foreground="{DynamicResource AppText}"></TextBlock>
<StackPanel Orientation="Horizontal" Margin="1,1,0,1">
<Button Content="{lex:Loc TaskCancel}" Margin="0,0,4,0" MinWidth="60" Height="28" FontSize="12" Click="btnCancelTask_Click" Background="{DynamicResource ActionButtonBackground}" Foreground="{DynamicResource ActionButtonText}" BorderBrush="{DynamicResource ActionButtonBorder}" />
<Button Visibility="{Binding Exception.Visibility}" Content="{lex:Loc TaskError}" Margin="0,0,4,0" MinWidth="60" Height="28" FontSize="12" Click="btnTaskError_Click" Background="{DynamicResource ActionButtonBackground}" Foreground="{DynamicResource ActionButtonText}" BorderBrush="{DynamicResource ActionButtonBorder}" />
<Button Visibility="{Binding Exception.Visibility}" Content="{lex:Loc TaskErrorButton}" Margin="0,0,4,0" MinWidth="60" Height="28" FontSize="12" Click="btnTaskError_Click" Background="{DynamicResource ActionButtonBackground}" Foreground="{DynamicResource ActionButtonText}" BorderBrush="{DynamicResource ActionButtonBorder}" />
<ProgressBar Height="16" Width="200" Minimum="0" Maximum="100" Value="{Binding Progress, Mode=OneWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Background="{DynamicResource AppElementInnerBackground}" Foreground="{DynamicResource ProgressBarForeground}" />
</StackPanel>
</StackPanel>
Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderWPF/PageQueue.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private void btnTaskError_Click(object sender, RoutedEventArgs e)
errorMessage = taskException.Exception.ToString();
}

MessageBox.Show(errorMessage, Translations.Strings.TaskError, MessageBoxButton.OK, MessageBoxImage.Error);
MessageBox.Show(errorMessage, Translations.Strings.MessageBoxTitleError, MessageBoxButton.OK, MessageBoxImage.Error);
}

private void btnRemoveTask_Click(object sender, RoutedEventArgs e)
Expand Down
2 changes: 2 additions & 0 deletions TwitchDownloaderWPF/Services/NativeFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace TwitchDownloaderWPF.Services
{
[SupportedOSPlatform("windows")]
public static class NativeFunctions
{
[DllImport("dwmapi.dll", EntryPoint = "DwmSetWindowAttribute", PreserveSig = true)]
Expand Down
6 changes: 5 additions & 1 deletion TwitchDownloaderWPF/Services/ThemeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Windows;
using System.Windows.Media;
using System.Xml.Serialization;
Expand All @@ -26,6 +27,7 @@ public ThemeService(App app, WindowsThemeService windowsThemeService)
{
Directory.CreateDirectory("Themes");
}

if (!DefaultThemeService.WriteIncludedThemes())
{
MessageBox.Show(Translations.Strings.ThemesFailedToWrite, Translations.Strings.ThemesFailedToWrite, MessageBoxButton.OK, MessageBoxImage.Information);
Expand Down Expand Up @@ -67,6 +69,7 @@ public void ChangeAppTheme()
{
newTheme = WindowsThemeService.GetWindowsTheme();
}

ChangeThemePath(newTheme);

var newSkin = _darkHandyControl ? SkinType.Dark : SkinType.Default;
Expand All @@ -78,6 +81,7 @@ public void ChangeAppTheme()
}
}

[SupportedOSPlatform("windows")]
public void SetTitleBarTheme(WindowCollection windows)
{
// If windows 10 build is before 1903, it doesn't support dark title bars
Expand Down Expand Up @@ -146,4 +150,4 @@ private void SetHandyControlTheme(SkinType newSkin)
_wpfApplication.Resources.MergedDictionaries[1].Source = new Uri($"pack://application:,,,/HandyControl;component/Themes/Theme.xaml", UriKind.Absolute);
}
}
}
}
17 changes: 14 additions & 3 deletions TwitchDownloaderWPF/Services/WindowsThemeService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Microsoft.Win32;
using System;
using System.Management;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Principal;
using System.Windows;

namespace TwitchDownloaderWPF.Services
{
[SupportedOSPlatform("windows")]
public class WindowsThemeService : ManagementEventWatcher
{
public event EventHandler<string> ThemeChanged;
Expand All @@ -17,8 +20,9 @@ public class WindowsThemeService : ManagementEventWatcher

public WindowsThemeService()
{
// If windows version is before windows 10 or the windows 10 build is before 1809, it doesn't have the app theme registry key
if (Environment.OSVersion.Version.Major < 10 || Environment.OSVersion.Version.Build < 17763)
// If the OS is older than Windows 10 1809 then it doesn't have the app theme registry key
const int WINDOWS_1809_BUILD_NUMBER = 17763;
if (Environment.OSVersion.Version.Major < 10 || Environment.OSVersion.Version.Build < WINDOWS_1809_BUILD_NUMBER)
{
return;
}
Expand All @@ -33,7 +37,14 @@ public WindowsThemeService()
Query = new EventQuery(windowsQuery);
EventArrived += WindowsThemeService_EventArrived;

Start();
try
{
Start();
}
catch (ExternalException e)
{
MessageBox.Show(string.Format(Translations.Strings.UnableToStartWindowsThemeWatcher, $"0x{e.ErrorCode:x8}"), Translations.Strings.MessageBoxTitleError, MessageBoxButton.OK, MessageBoxImage.Error);
}
}

private void WindowsThemeService_EventArrived(object sender, EventArrivedEventArgs e)
Expand Down
22 changes: 20 additions & 2 deletions TwitchDownloaderWPF/Translations/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion TwitchDownloaderWPF/Translations/Strings.es.resx
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@
<data name="TaskCancel" xml:space="preserve">
<value>Cancelar</value>
</data>
<data name="TaskError" xml:space="preserve">
<data name="TaskErrorButton" xml:space="preserve">
<value>Error</value>
</data>
<data name="TaskQueue" xml:space="preserve">
Expand Down Expand Up @@ -766,4 +766,10 @@
<data name="EncodeClipMetadata" xml:space="preserve">
<value>Codificar metadatos:</value>
</data>
<data name="MessageBoxTitleError" xml:space="preserve">
<value>Error</value>
</data>
<data name="UnableToStartWindowsThemeWatcher" xml:space="preserve">
<value>Unable to start Windows application theme watcher. Error code: {0}</value>
</data>
</root>
8 changes: 7 additions & 1 deletion TwitchDownloaderWPF/Translations/Strings.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@
<data name="TaskCancel" xml:space="preserve">
<value>Annulation</value>
</data>
<data name="TaskError" xml:space="preserve">
<data name="TaskErrorButton" xml:space="preserve">
<value>Erreur</value>
</data>
<data name="TaskQueue" xml:space="preserve">
Expand Down Expand Up @@ -765,4 +765,10 @@
<data name="EncodeClipMetadata" xml:space="preserve">
<value>Inclure les métadonnées</value>
</data>
<data name="MessageBoxTitleError" xml:space="preserve">
<value>Erreur</value>
</data>
<data name="UnableToStartWindowsThemeWatcher" xml:space="preserve">
<value>Impossible de démarrer l'observateur de thème de l'application Windows. Code d'erreur : {0}</value>
</data>
</root>
8 changes: 7 additions & 1 deletion TwitchDownloaderWPF/Translations/Strings.pl.resx
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@
<data name="TaskCancel" xml:space="preserve">
<value>Anuluj</value>
</data>
<data name="TaskError" xml:space="preserve">
<data name="TaskErrorButton" xml:space="preserve">
<value>Błąd</value>
</data>
<data name="TaskQueue" xml:space="preserve">
Expand Down Expand Up @@ -765,4 +765,10 @@
<data name="EncodeClipMetadata" xml:space="preserve">
<value>Encode Metadata:</value>
</data>
<data name="MessageBoxTitleError" xml:space="preserve">
<value>Błąd</value>
</data>
<data name="UnableToStartWindowsThemeWatcher" xml:space="preserve">
<value>Unable to start Windows application theme watcher. Error code: {0}</value>
</data>
</root>
8 changes: 7 additions & 1 deletion TwitchDownloaderWPF/Translations/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@
<data name="TaskCancel" xml:space="preserve">
<value>Cancel</value>
</data>
<data name="TaskError" xml:space="preserve">
<data name="TaskErrorButton" xml:space="preserve">
<value>Error</value>
</data>
<data name="TaskQueue" xml:space="preserve">
Expand Down Expand Up @@ -764,4 +764,10 @@
<data name="EncodeClipMetadata" xml:space="preserve">
<value>Encode Metadata:</value>
</data>
<data name="MessageBoxTitleError" xml:space="preserve">
<value>Error</value>
</data>
<data name="UnableToStartWindowsThemeWatcher" xml:space="preserve">
<value>Unable to start Windows application theme watcher. Error code: {0}</value>
</data>
</root>
8 changes: 7 additions & 1 deletion TwitchDownloaderWPF/Translations/Strings.ru.resx
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@
<data name="TaskCancel" xml:space="preserve">
<value>Отмена</value>
</data>
<data name="TaskError" xml:space="preserve">
<data name="TaskErrorButton" xml:space="preserve">
<value>Ошибка</value>
</data>
<data name="TaskQueue" xml:space="preserve">
Expand Down Expand Up @@ -765,4 +765,10 @@
<data name="EncodeClipMetadata" xml:space="preserve">
<value>Encode Metadata:</value>
</data>
<data name="MessageBoxTitleError" xml:space="preserve">
<value>Ошибка</value>
</data>
<data name="UnableToStartWindowsThemeWatcher" xml:space="preserve">
<value>Unable to start Windows application theme watcher. Error code: {0}</value>
</data>
</root>
8 changes: 7 additions & 1 deletion TwitchDownloaderWPF/Translations/Strings.tr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@
<data name="TaskCancel" xml:space="preserve">
<value>İptal</value>
</data>
<data name="TaskError" xml:space="preserve">
<data name="TaskErrorButton" xml:space="preserve">
<value>Hata</value>
</data>
<data name="TaskQueue" xml:space="preserve">
Expand Down Expand Up @@ -766,4 +766,10 @@
<data name="EncodeClipMetadata" xml:space="preserve">
<value>Encode Metadata:</value>
</data>
<data name="MessageBoxTitleError" xml:space="preserve">
<value>Hata</value>
</data>
<data name="UnableToStartWindowsThemeWatcher" xml:space="preserve">
<value>Unable to start Windows application theme watcher. Error code: {0}</value>
</data>
</root>
8 changes: 7 additions & 1 deletion TwitchDownloaderWPF/Translations/Strings.zh.resx
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@
<data name="TaskCancel" xml:space="preserve">
<value>取消</value>
</data>
<data name="TaskError" xml:space="preserve">
<data name="TaskErrorButton" xml:space="preserve">
<value>错误</value>
</data>
<data name="TaskQueue" xml:space="preserve">
Expand Down Expand Up @@ -764,4 +764,10 @@
<data name="EncodeClipMetadata" xml:space="preserve">
<value>Encode Metadata:</value>
</data>
<data name="MessageBoxTitleError" xml:space="preserve">
<value>错误</value>
</data>
<data name="UnableToStartWindowsThemeWatcher" xml:space="preserve">
<value>Unable to start Windows application theme watcher. Error code: {0}</value>
</data>
</root>