Skip to content

Commit 2ccf292

Browse files
committed
Better error handling if clipboard is locked
1 parent b6eafc9 commit 2ccf292

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/Clowd.Clipboard/ClipboardHandle.cs

+15-16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public void Open()
3737
if (--i == 0) ThrowOpenFailed();
3838
Thread.Sleep(RETRY_DELAY);
3939
}
40+
4041
_isOpen = true;
4142
}
4243

@@ -53,6 +54,7 @@ public async Task OpenAsync()
5354
if (--i == 0) ThrowOpenFailed();
5455
await Task.Delay(RETRY_DELAY).ConfigureAwait(false);
5556
}
57+
5658
_isOpen = true;
5759
}
5860

@@ -393,30 +395,27 @@ protected void ThrowOpenFailed()
393395
var hr = Marshal.GetLastWin32Error();
394396
var mex = Marshal.GetExceptionForHR(hr);
395397

396-
if (hr == 5) // ACCESS DENIED
398+
try
397399
{
398-
IntPtr hwnd = NativeMethods.GetOpenClipboardWindow();
399-
if (hwnd != IntPtr.Zero)
400+
if (hr == 5) // ACCESS DENIED
400401
{
401-
uint threadId = NativeMethods.GetWindowThreadProcessId(hwnd, out var processId);
402-
string processName = "Unknown";
403-
try
402+
IntPtr hwnd = NativeMethods.GetOpenClipboardWindow();
403+
if (hwnd != IntPtr.Zero)
404404
{
405+
uint threadId = NativeMethods.GetWindowThreadProcessId(hwnd, out var processId);
406+
string processName = "Unknown";
405407
var p = Process.GetProcessById((int)processId);
406408
processName = p.ProcessName;
409+
throw new ClipboardBusyException((int)processId, processName, mex);
407410
}
408-
catch { }
409-
410-
throw new ClipboardBusyException((int)processId, processName, mex);
411-
412-
}
413-
else
414-
{
415-
throw new ClipboardBusyException(mex);
416411
}
417412
}
413+
catch
414+
{
415+
// if we can't get the process name locking clipboard, just throw the original exception
416+
}
418417

419-
throw mex;
418+
throw new ClipboardBusyException(mex);
420419
}
421420
}
422-
}
421+
}

0 commit comments

Comments
 (0)