Requested Clipboard operation did not succeed

淺唱寂寞╮ 提交于 2019-11-29 11:46:18

问题


Exception Type: ExternalException

Message: Requested Clipboard operation did not succeed.

Method: ThrowIfFailed

Source: System.Windows.Forms



Stack Trace:

   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)
   at Deerfield.Base.Controls.DataGridView.ProcessCmdKey(Message& msg, Keys keyData) in C:\Users\Developer\Desktop\deerfield\src\core\Deerfield\Deerfield.Base\Controls\DataGridView.cs:line 555
   at System.Windows.Forms.Control.ProcessCmdKey(Message& msg, Keys keyData)
   at System.Windows.Forms.Control.ProcessCmdKey(Message& msg, Keys keyData)
   at System.Windows.Forms.TextBoxBase.ProcessCmdKey(Message& msg, Keys keyData)
   at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
   at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
   at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)

I googled this, but I cannot get a decent answer as to why this is happening.

The MSDN documentation says that this often happens when the user switches to another application, but it does not appear that this was the case.


回答1:


Having a similar problem. Found this entry, which basically says to set retryTimes to 2 in the call:

Clipboard.SetDataObject(object data, bool copy, int retryTimes, int retryDelay)

Going to try it. Would be nice if anyone could post a reproducible test case.




回答2:


The root cause is likely to be that you are doing two operations, typically a copy and a paste, and assume that the clipboard will be available. What happens is, that you do a copy (to update the clipboard) and then other clipboard viewers are reacting to it when you try to paste. The defense is to have an except/sleep/retry mechanism around the paste operation, so that you can handle it gracefully. Telling the user to shut down rpdclip and such, won't fly in a production application. Also make sure that you're not (ab)using the clipboard as a crutch. The clipboard is provided for the convenience of the USER, not the PROGRAMMER.




回答3:


I had this problem with an app but only when running it on an HP mini.

If I have C# express running so I can check the exception,

shutting down Google Chrome removes the problem.

reopening Google Chrome causes it to reappear.

But on my main 64 bit machine, no problem; and on my previous 32 bit machine, no problem either. Side effect of limited RAM perhaps?

Gerald




回答4:


EASY! I had the same issue and fixed it.

Just open Task Manager, search for rdpclip.exe under Processes, kill it. Then, open a new task and run it again.




回答5:


It's some other application is using Clipboard now. Find out the application monitoring Clipboard and kill the process. Works for me.




回答6:


I had this problem too, and use this code as WireGuy answered. but this code code makes an exception in my PC "Requested Clipboard operation did not succeed". I put this line a Try Catch statement

            try
            {
                Clipboard.SetDataObject(textBoxCodePan.Text, true, 10, 100);
            }
            catch (Exception)
            {

            }

and did work correctly.




回答7:


I used System.Windows.Forms.Control.WndProc method and PostMessage.

string clipboardText;

{
    clipboardText = "TEXT FOR CLIPBOARD";
    PostMessage(Handle, CLIPBOARD_BACKUP_MSG, 0, 0);
}

protected override void WndProc(ref Message m) 
{
    if (m.Msg == CLIPBOARD_BACKUP_MSG)
    {
        Clipboard.SetText(clipboardText);
    }

    base.WndProc(ref m);
}



回答8:


For myself, I found that the clipboard was still processing my request while I was putting a new one. SendKeys.SendWait("^c"); Clipboard.GetText();

So I added Sleep and it now works great. SendKeys.SendWait("^c"); Thread.Sleep(250); Clipboard.GetText();




回答9:


If you are using some VNC program (RealVNC) and your application use Clipboard from System.Windows.Forms.dll on main thread "The requested clipboard operation failed" will occur. This is my solution written in C# for .NET 3.5:

using System.Threading;

   var dataObject = new DataObject();
   private Clipboard()
   {
        //dataObject logic here

        Thread clipboardThread = new Thread(new ThreadStart(GetClipboard));
        clipboardThread.SetApartmentState(ApartmentState.STA);
        clipboardThread.Start();
   }

   private void GetClipboard()
   {
         Clipboard.SetDataObject(dataObject, true, 10, 100);
   }



回答10:


For some reason i got "Requested Clipboard operation did not succeed" exceptions every time when running

Dim s = "test"
Clipboard.SetDataObject(s, True, 10, 200)

But

Dim s = "test"
Clipboard.ContainsText()
Clipboard.SetDataObject(s, True, 10, 200)

worked every time.

However, interestingly

Try
    Dim s = "test"
    Clipboard.SetDataObject(s, True, 10, 200)
catch ex as exception
    Dim s = "test"
    Clipboard.ContainsText()
    Clipboard.SetDataObject(s, True, 10, 200)
end try

will fail on both SetDataObject calls

I'm sure its as transient error as i was setting clipboard content the other day without issue.



来源:https://stackoverflow.com/questions/5707990/requested-clipboard-operation-did-not-succeed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!