How to handle a blocked clipboard and other weird things

Over the past two hours, I have been tracking a fairly specific error, as another application has opened the clipboard. Essentially, since the clipboard is a shared resource (according to "Why is my shared clipboard not working?" And you are trying to execute

Clipboard.SetText(string) 

or

 Clipboard.Clear(). 

The following exception is thrown:

 System.Runtime.InteropServices.ExternalException: Requested Clipboard operation did not succeed. 
     at System.Windows.Forms.Clipboard.ThrowIfFailed (Int32 hr)
     at System.Windows.Forms.Clipboard.SetDataObject (Object data, Boolean copy, Int32 retryTimes, Int32 retryDelay)
     at System.Windows.Forms.Clipboard.SetText (String text, TextDataFormat format)
     at System.Windows.Forms.Clipboard.SetText (String text)

My initial decision was to retry after a short pause until I realized that Clipboard.SetDataObject has fields for the number of times and length of the delay. The default behavior of .NET is to try 10 times with a delay of 100 ms.

There is one final thing that was noted by the end user, despite the fact that the exception causes the copy operation to the clipboard, but I could not find any additional information about why this might be.

My current solution to the problem is to just silently ignore the exception ... is this really the best way?

+45
c # clipboard
May 30 '09 at 17:57
source share
8 answers

Since the clipboard is used by all UI applications, you will encounter this from time to time, it is obvious that you do not want your application to crash if it could not write to the clipboard, so it is smart to handle ExternalException intelligently, I will offer the user a view and an error if calling setobjectdata to write to the clipboard fails.

It was suggested to use (via p / invoke) user32!GetOpenClipboardWindow to see if another application is open to open the clipboard, it will return the HWND of the window in which the clipboard is open, or IntPtr.Zero , if the application was not open, you can rotate value until IntPtr.Zero for a certain period of time.

+23
May 30 '09 at 18:11
source share

I'm sorry to resurrect the old question, but another workaround would be to use Clipboard.SetDataObject instead of Clipboard.SetText .

According to this msdn article, this method has two parameters: retryTimes and retryDelay - which you can use like this:

 Clipboard.SetDataObject( "some text", //text to store in clipboard false, //do not keep after our app exits 5, //retry 5 times 200); //200ms delay between retries 
+37
Mar 05 '11 at 12:32
source share

Today I ran into this error. I decided to handle this by informing the user of a potentially erroneous application. For this you can do something like this:

 [System.Runtime.InteropServices.DllImport("user32.dll")] static extern IntPtr GetOpenClipboardWindow(); [System.Runtime.InteropServices.DllImport("user32.dll")] static extern int GetWindowText(int hwnd, StringBuilder text, int count); private void btnCopy_Click(object sender, EventArgs e) { try { Clipboard.Clear(); Clipboard.SetText(textBox1.Text); } catch (Exception ex) { string msg = ex.Message; msg += Environment.NewLine; msg += Environment.NewLine; msg += "The problem:"; msg += Environment.NewLine; msg += getOpenClipboardWindowText(); MessageBox.Show(msg); } } private string getOpenClipboardWindowText() { IntPtr hwnd = GetOpenClipboardWindow(); StringBuilder sb = new StringBuilder(501); GetWindowText(hwnd.ToInt32(), sb, 500); return sb.ToString(); // example: // skype_plugin_core_proxy_window: 02490E80 } 

For me, the title of the problem window was "skype_plugin_core_proxy_window". I looked for information about it and was surprised that it gave only one blow, and it was in Russian. Therefore, I am adding this answer to give another hit for this line and provide additional help to bring potentially dangerous lighting applications.

+9
Oct 12 2018-11-12T00:
source share

Running Clipboard.Clear() before Clipboard.SetDataObject(pasteString, true) does the trick.

The previous suggestion of setting retryTimes and retryDelay did not work for me, and in any case the default values ​​are retryTimes = 10 and retryDelay = 100ms

+1
May 25 '12 at 13:58
source share

It's a little crappy, but it solved my problem.

Try cleaning again () after a delay.

Read more @ this blog.

0
Aug 17 2018-11-11T00:
source share

Just call first:

 [System.Runtime.InteropServices.DllImport("user32.dll")] static extern IntPtr CloseClipboard(); 

I noticed that if you are in the middle of an insert operation (WM_PASTE message), including during the TextChanged event, the clipboard remains a blocked window (TextBox) that accepts the event. Therefore, if you simply call this CloseClipboard method inside the event handler, you can call the Clipboard.Clear and Clipboard.SetText managed methods without any problems or delays.

0
Oct 10
source share

Using the Jeff Code

 [System.Runtime.InteropServices.DllImport("user32.dll")] static extern IntPtr GetOpenClipboardWindow(); [System.Runtime.InteropServices.DllImport("user32.dll")] static extern int GetWindowText(int hwnd, StringBuilder text, int count); private void btnCopy_Click(object sender, EventArgs e) { try { Clipboard.Clear(); Clipboard.SetText(textBox1.Text); } catch (Exception ex) { string msg = ex.Message; msg += Environment.NewLine; msg += Environment.NewLine; msg += "The problem:"; msg += Environment.NewLine; msg += getOpenClipboardWindowText(); MessageBox.Show(msg); } } private string getOpenClipboardWindowText() { IntPtr hwnd = GetOpenClipboardWindow(); StringBuilder sb = new StringBuilder(501); GetWindowText(hwnd.ToInt32(), sb, 500); return sb.ToString(); // example: // skype_plugin_core_proxy_window: 02490E80 } 

You can handle the error in a rather convenient way.

I was able to reduce the error rate using System.Windows.Forms.Clipboard instead of System.Windows.Clipboard .

I emphasize that this does not fix the problem, but it reduced the occurrence of my application.

0
Jul 24. '13 at 13:03
source share

So, I really came up with my solution. I know I'm a little late, HOWEVER, it seems like this works for me.

 // This allows the clipboard to have something copied to it. public static void ClipboardPaste(String pasteString) { // This clears the clipboard Clipboard.Clear(); // This is a "Try" of the statement inside {}, if it fails it goes to "Catch" // If it "Catches" an exception. Then the function will retry itself. try { // This, per some suggestions I found is a half second second wait before another // clipboard clear and before setting the clipboard System.Threading.Thread.Sleep(500); Clipboard.Clear(); // This is, instead of using SetText another method to put something into // the clipboard, it includes a retry/fail set up with a delay // It retries 5 times with 250 milliseconds (0.25 second) between each retry. Clipboard.SetDataObject(pasteString, false, 5, 250); } catch (Exception) { ClipboardPaste(pasteString); } } 

This is obviously C #, however these methods are available to all Visual Studios. I obviously created a looping function, and also tried to force it into the clipboard using attempts.

Essentially there is a stream. Suppose you want to put the word Clipboard on the clipboard anywhere in your code (provided that this function is defined).

  • Call function ClipboardPaste ("Clipboard");
  • Then clear the clipboard
  • Then it will "try" to put your string on the clipboard.
  • First, it waits for half a second (500 milliseconds).
  • Resets clipboard again
  • Then it tries to put the string on the clipboard using SetDataObject
  • SetDataObject, if it fails, will retry up to 5 times with a delay of 250 milliseconds between each attempt.
  • If the initial attempt failed, it detects an exception, abnormal termination, then tries again.

Yes, this has a drawback if you know that your clipboard will always have an exception no matter what (an infinite loop). However, I have not yet encountered an infinite loop with this method. Another drawback is that it can take a couple of seconds (significantly slowing down your applications) before it works, while when you try to freeze your application, as soon as it works, the application will continue to work anyway.

-3
Mar 04 2018-12-21T00:
source share



All Articles