Skip to content

Commit 8db540f

Browse files
committed
Better disposing of icon handle
1 parent c97f49b commit 8db540f

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

XBatteryStatus/MyApplicationContext.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,22 @@ public bool IsLightMode()
548548
public void SetIcon(int val, string s = "")
549549
{
550550
Icon oldIcon = notifyIcon.Icon;
551-
notifyIcon.Icon = GetIcon(val, s); ;
551+
IntPtr oldHandle = IntPtr.Zero;
552+
553+
if (oldIcon != null)
554+
{
555+
oldHandle = oldIcon.Handle;
556+
}
557+
558+
notifyIcon.Icon = GetIcon(val, s);
552559

553560
if (oldIcon != null)
554561
{
555-
IntPtr oldHandle = oldIcon.Handle;
556562
oldIcon.Dispose();
557-
DestroyIcon(oldHandle);
563+
if (oldHandle != IntPtr.Zero)
564+
{
565+
DestroyIcon(oldHandle);
566+
}
558567
}
559568
}
560569

0 commit comments

Comments
 (0)