Skip to content

Commit 75d7d07

Browse files
committed
Add retries when accessing DesktopWallpaper COM class
1 parent 40cb4ec commit 75d7d07

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/COM/DesktopWallpaper.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Runtime.InteropServices;
7+
using System.Threading;
78

89
namespace WinDynamicDesktop.COM
910
{
@@ -100,9 +101,23 @@ public static class DesktopWallpaperFactory
100101
[Guid("C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD")]
101102
class DesktopWallpaperCoclass { }
102103

104+
private const int REGDB_E_CLASSNOTREG = 0x80040154;
105+
103106
public static IDesktopWallpaper Create()
104107
{
105-
return (IDesktopWallpaper)new DesktopWallpaperCoclass();
108+
for (int attempt = 0; ; attempt++)
109+
{
110+
try
111+
{
112+
return (IDesktopWallpaper)new DesktopWallpaperCoclass();
113+
}
114+
catch (COMException e) when (e.HResult == REGDB_E_CLASSNOTREG && attempt < 3)
115+
{
116+
LoggingHandler.LogMessage("Accessing DesktopWallpaper COM class failed, retrying ({1}/3)",
117+
attempt + 1);
118+
Thread.Sleep(1000);
119+
}
120+
}
106121
}
107122
}
108123
}

0 commit comments

Comments
 (0)