Skip to content

Commit b62180e

Browse files
committed
Try catch TaskbarList
1 parent f47b53f commit b62180e

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

TaskbarListCOM/TaskbarList.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,54 @@ namespace Hi3Helper.Win32.TaskbarListCOM
66
{
77
public class TaskbarList
88
{
9-
private ITaskbarList3? m_taskbarList;
9+
private readonly ITaskbarList3? m_taskbarList;
1010

1111
public TaskbarList()
1212
{
13-
ComMarshal.CreateInstance(new Guid(CLSIDGuid.CLSID_TaskbarList),
14-
0,
15-
CLSCTX.CLSCTX_INPROC_SERVER,
16-
out ITaskbarList3? taskbarList).ThrowOnFailure();
17-
m_taskbarList = taskbarList;
13+
try
14+
{
15+
ComMarshal.CreateInstance(new Guid(CLSIDGuid.CLSID_TaskbarList),
16+
0,
17+
CLSCTX.CLSCTX_INPROC_SERVER,
18+
out ITaskbarList3? taskbarList).ThrowOnFailure();
19+
m_taskbarList = taskbarList;
20+
}
21+
catch (Exception)
22+
{
23+
// ignore
24+
}
1825
}
1926

2027
~TaskbarList()
2128
{
22-
ComMarshal.FreeInstance(m_taskbarList);
29+
if (m_taskbarList != null)
30+
{
31+
ComMarshal.FreeInstance(m_taskbarList);
32+
}
2333
}
2434

2535
public int SetProgressState(nint hwnd, TaskbarState state)
2636
{
27-
return m_taskbarList!.SetProgressState(hwnd, state);
37+
try
38+
{
39+
return m_taskbarList?.SetProgressState(hwnd, state) ?? 0;
40+
}
41+
catch (Exception)
42+
{
43+
return 0;
44+
}
2845
}
2946

3047
public int SetProgressValue(nint hwnd, ulong ullCompleted, ulong ullTotal)
3148
{
32-
return m_taskbarList!.SetProgressValue(hwnd, ullCompleted, ullTotal);
49+
try
50+
{
51+
return m_taskbarList?.SetProgressValue(hwnd, ullCompleted, ullTotal) ?? 0;
52+
}
53+
catch (Exception)
54+
{
55+
return 0;
56+
}
3357
}
3458
}
3559
}

0 commit comments

Comments
 (0)