0

I have some problems with the clipboard. Often when I try to access it (reading or writing data does not matter) I got ExternalExceptions. I know why exceptions with the error code CLIPBRD_E_CANT_OPEN happens, so I caught these exceptions. But now I get other Exceptions with the error code E_FAIL, which means unspecified failure. I do not like the idea to catch these to. This is like catching Exeception and do nothing.

Even if I catch these I get the message described here. Unfortunate there was no answer given. Can someone give a solution for this or explain why this happens? I will give you my clipboard handling code, but I don't think this is the problem here.

internal class ClipboardHandle
{
    private const int MaxTries = 5;
    private const int DelayBetweenTries = 200;
    private const int CantOpenClipboardCode = -2147221040;
    private const int UnspecifiedFailure = -2147467259;
    private readonly Timer setDataTimer = new Timer(DelayBetweenTries) { AutoReset = false };
    private int setDataTries;
    private DataObject clipboardDataObject;

    public ClipboardHandle()
    {
        setDataTimer.Elapsed += OnSetDataTimerElapsed;
    }

    internal void SetDataObject(DataObject clipboardData)
    {
        setDataTimer.Enabled = false;
        setDataTries = 0;
        clipboardDataObject = clipboardData;

        SetClipboardData();
    }

    private void SetClipboardData()
    {
        try
        {
            System.Windows.Clipboard.SetDataObject(clipboardDataObject,false);
            clipboardDataObject = null;
        }
        catch (ExternalException e)
        {
            if (e.ErrorCode != CantOpenClipboardCode && e.ErrorCode != UnspecifiedFailure) throw;
            if (setDataTries < MaxTries)
            {
                setDataTries++;
                setDataTimer.Enabled = true;
            }
        }
    }

    private void OnSetDataTimerElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
    {
        SetClipboardData();
    }

    internal bool ContainsData(string key)
    {
        try
        {
            return System.Windows.Clipboard.ContainsData(key);
        }
        catch (ExternalException e)
        {
            if (e.ErrorCode != CantOpenClipboardCode && e.ErrorCode != UnspecifiedFailure) throw;
        }
        return false;
    }

    internal object GetData(string key)
    {
        try
        {
            return System.Windows.Clipboard.GetData(key);
        }
        catch (ExternalException e)
        {
            if (e.ErrorCode != CantOpenClipboardCode && e.ErrorCode != UnspecifiedFailure) throw;
        }
        return null;
    }
}

EDIT

I just found a new exception. After letting the program run for several hours and let the computer go to sleep, wake him up again. I got an OutOfMemoryException while reading the clipboard.

For information: The DataObject has an program specific object and parallel some text which I could paste in any editor.

Community
  • 1
  • 1
Yggdrasil
  • 1,377
  • 2
  • 13
  • 27
  • CLIPBRD_E_CANT_OPEN is already bad news. At least use TaskMgr.exe and start killing processes on your machine one by one to make sure you are not fighting one that just makes your life miserable. If that doesn't help then you'll need to be a lot more specific about what kind of object you're trying to put on the clipboard. – Hans Passant Jul 24 '13 at 16:34
  • We know the program which tries to open the clipboard at the same time. This is inevitable. For me this is the only exception which is ok to catch and try again if it is set. The others gives me headaches. – Yggdrasil Jul 25 '13 at 10:29

0 Answers0