Occasional "System.Exception: OpenClipboard Failed (Exception from HRESULT: 0x800401D0)"

Hong 1,351 Reputation points
2024-06-13T08:40:09.66+00:00

An app occasionally throws the following exception:

System.Exception: OpenClipboard Failed (Exception from HRESULT: 0x800401D0)
 Stack Trace:
   at System.Runtime.InteropServices.McgMarshal.ThrowOnExternalCallFailed(Int32, RuntimeTypeHandle) + 0x21
   at __Interop.ComCallHelpers.Call(__ComObject, RuntimeTypeHandle, Int32, Void*) + 0xbe
   at __Interop.ForwardComStubs.Stub_9[TThis, TArg0](__ComObject, TArg0, Int32) + 0x5a
   at ClassLibraryUno.Utility.<>c__DisplayClass119_0.<CopyToClipboard>b__0() + 0x34
   at Windows.Foundation.DeferralCompletedHandler.Invoke() + 0x25
   at Camera_Alternative!<BaseAddress>+0x28dfd9c


DataPackage dp = new DataPackage();
dp.SetText(sText);
await dispatcher.RunAsync(
    CoreDispatcherPriority.Normal,
    () =>
    {
        Clipboard.SetContent(dp);
    });

Could anyone offer a tip on the possible cause?

Developer technologies | Universal Windows Platform (UWP)
{count} votes

Answer accepted by question author
  1. Harry Vo (WICLOUD CORPORATION) 3,355 Reputation points Microsoft External Staff Moderator
    2025-07-17T08:40:18.0033333+00:00

    Hi Hong,

    This error (0x800401D0) indicates that the clipboard is temporarily unavailable — usually because another process or thread is accessing it at the same time. Since the clipboard is a shared system resource, this can cause occasional race conditions.

    As David Lowndes mentioned in comment, there is a common workaround which is to add a retry loop with a short delay when calling Clipboard.SetContent, you can apply that solution like this:

    var dp = new DataPackage();
    dp.SetText(sText);
    int retries = 3; // Edit number of retries if needed
    while (retries-- > 0)
    {
    	try
    	{
    		await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    		{
    			Clipboard.SetContent(dp);
    		});
    		break; // Success
    	}
    	catch (Exception ex) when ((uint)ex.HResult == 0x800401D0)
    	{
    		// Clipboard is busy, wait and retry
    		await Task.Delay(100);
    	}
    }
    

    If you have any question, feel free to ask! Thank you!

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.