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.