C#C
C#5mo ago
faint

First use of Winforms' Clipboard methods results in an ExternalException

Was debugging a Winforms app bug where the following code would fail but only if it is the first time of using any Clipboard method.
private static readonly object ClipboardLock = new object();
private static bool CopyData(IDataObject data, bool copy = true)
{
    if (data == null)
        return false;

    lock (ClipboardLock)
        Clipboard.SetDataObject(data, copy, 20, 100);

    return true;
}

When used for the first time since the app launch, it throws the following exception: System.Runtime.InteropServices.ExternalException (0x800401D0): Requested Clipboard operation did not succeed

Interestingly enough, if you just add a random Clipboard method BEFORE the SetDataObject call, everything works fine without any errors

Could someone help me to figure out what might be causing it? Thanks in advance!
Was this page helpful?