Skip to content

Commit

Permalink
Properly handle COMException and removed locking status check to avoi…
Browse files Browse the repository at this point in the history
…d potential dead locks.
  • Loading branch information
leonzhou-smokeball committed Jan 3, 2025
1 parent 722049f commit 3d6444c
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 175 deletions.
2 changes: 1 addition & 1 deletion App/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<SupportedOSPlatformVersion>10.0.22000.0</SupportedOSPlatformVersion>
<ApplicationHighDpiMode>PerMonitorV2</ApplicationHighDpiMode>
<Platforms>AnyCPU;x64;ARM64;x86</Platforms>
<PackageVersion>2.1.6</PackageVersion>
<PackageVersion>2.1.7</PackageVersion>
<PackageProjectUrl>https://github.com/soleon/Percentage</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/soleon/Percentage?tab=MIT-1-ov-file</PackageLicenseUrl>
<PackageIcon>Icon.png</PackageIcon>
Expand Down
2 changes: 1 addition & 1 deletion App/Extensions/ApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static MainWindow ActivateMainWindow(this Application app)
return window;
}

internal static NotifyIconWindow GetTrayIconWindow(this Application app)
internal static NotifyIconWindow GetNotifyIconWindow(this Application app)
{
return app.Windows.OfType<NotifyIconWindow>().FirstOrDefault();
}
Expand Down
2 changes: 1 addition & 1 deletion App/Extensions/DelegateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Percentage.App.Extensions;

internal static class DelegateExtensions
{
private const int DefaultMaxRetryCount = 5;
private const int DefaultMaxRetryCount = 3;

internal static void RetryOnException<T>(this Action action, Action<T> onRetryFailed = null,
int maxRetryCount = DefaultMaxRetryCount) where T : Exception
Expand Down
29 changes: 14 additions & 15 deletions App/Extensions/NotifyIconExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,20 @@ internal static void SetIcon(this NotifyIcon notifyIcon, FrameworkElement textBl

// Render the element with the correct DPI scale.
var dpiScale = VisualTreeHelper.GetDpi(textBlock);
var renderTargetBitmap = new RenderTargetBitmap(
(int)Math.Round(DefaultNotifyIconSize * dpiScale.DpiScaleX, MidpointRounding.AwayFromZero),
(int)Math.Round(DefaultNotifyIconSize * dpiScale.DpiScaleY, MidpointRounding.AwayFromZero),
dpiScale.PixelsPerInchX,
dpiScale.PixelsPerInchY,
PixelFormats.Default);

// There's a chance that a COMException can be thrown when rendering the bitmap.
// Catch any COM exceptions here and retry a few times then fail silently.
DelegateExtensions.RetryOnException<COMException>(() => renderTargetBitmap.Render(textBlock),
App.SetTrayIconUpdateError);

// There's a chance that some native exception may be thrown when setting the icon's image.

// There's a chance that some native exceptions may be thrown when rendering and setting the icon's image.
// Catch any exception here and retry a few times then fail silently.
DelegateExtensions.RetryOnException<Exception>(() => notifyIcon.Icon = renderTargetBitmap,
App.SetTrayIconUpdateError);
DelegateExtensions.RetryOnException<Exception>(() =>
{
var renderTargetBitmap = new RenderTargetBitmap(
(int)Math.Round(DefaultNotifyIconSize * dpiScale.DpiScaleX, MidpointRounding.AwayFromZero),
(int)Math.Round(DefaultNotifyIconSize * dpiScale.DpiScaleY, MidpointRounding.AwayFromZero),
dpiScale.PixelsPerInchX,
dpiScale.PixelsPerInchY,
PixelFormats.Default);
renderTargetBitmap.Render(textBlock);
notifyIcon.Icon = renderTargetBitmap;
App.SetTrayIconUpdateError(null);
}, App.SetTrayIconUpdateError);
}
}
286 changes: 131 additions & 155 deletions App/NotifyIconWindow.xaml.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion App/Pages/DetailsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public DetailsPage()
private void OnRefreshButtonClick(object sender, RoutedEventArgs e)
{
BatteryInformation.RequestUpdate();
Application.Current.GetTrayIconWindow().RequestBatteryStatusUpdate();
Application.Current.GetNotifyIconWindow().RequestBatteryStatusUpdate();
}
}
2 changes: 1 addition & 1 deletion Pack/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Identity
Name="61867SoleonInnovation.BatteryPercentageIcon"
Publisher="CN=B0B1FE5B-CC73-4F71-BD3F-7B809647826C"
Version="2.1.6.0" />
Version="2.1.7.0" />

<Properties>
<DisplayName>Battery Percentage Icon</DisplayName>
Expand Down

0 comments on commit 3d6444c

Please sign in to comment.