请求的剪贴板操作未成功

发布于 2024-11-02 17:04:07 字数 1299 浏览 4 评论 0原文

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)

我用谷歌搜索了这个,但我无法得到关于为什么会发生这种情况的正确答案。

MSDN 文档称,当用户切换到另一个应用程序时,这种情况经常发生,但事实似乎并非如此。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

小苏打饼 2024-11-09 17:04:07

有类似的问题。找到此条目 ,
基本上就是说在调用中将 retryTimes 设置为 2:

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

去尝试一下。如果有人可以发布可重现的测试用例,那就太好了。

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.

得不到的就毁灭 2024-11-09 17:04:07

根本原因可能是您正在执行两项操作,通常是复制和粘贴,并假设剪贴板可用。发生的情况是,您进行复制(以更新剪贴板),然后当您尝试粘贴时其他剪贴板查看器会对它做出反应。防御方法是在粘贴操作周围有一个例外/睡眠/重试机制,以便您可以优雅地处理它。告诉用户关闭 rpdclip 等,在生产应用程序中是行不通的。
还要确保您没有将剪贴板用作拐杖。提供剪贴板是为了方便用户,而不是程序员。

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.

假面具 2024-11-09 17:04:07

简单的!我有同样的问题并解决了它。

只需打开任务管理器,在进程下搜索rdpclip.exe,将其杀死。然后,打开一个新任务并再次运行它。

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.

苍风燃霜 2024-11-09 17:04:07

我在使用某个应用程序时遇到了这个问题,但仅在 HP mini 上运行该应用程序时遇到。

如果我正在运行 C# Express,以便可以检查异常,

则关闭 Google Chrome 可以消除该问题。

重新打开 Google Chrome 会导致它重新出现。

但在我的主 64 位机器上,没问题;在我以前的 32 位机器上也没有问题。也许是有限内存的副作用?

杰拉尔德

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

爱她像谁 2024-11-09 17:04:07

现在有其他应用程序正在使用剪贴板。找出监控剪贴板的应用程序并杀死该进程。对我有用。

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

花开雨落又逢春i 2024-11-09 17:04:07

如果您正在使用某些 VNC 程序 (RealVNC) 并且您的应用程序在主线程上使用 System.Windows.Forms.dll 中的剪贴板,则会发生“请求的剪贴板操作失败”。这是我用 C# 为 .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);
   }

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);
   }
空气里的味道 2024-11-09 17:04:07

我遇到了这个问题,这对我有用,在将文本设置到剪贴板(Clipboard.SetText(string Text))之前添加这个,

所以它最终变成

Clipboard.Clear();
Clipboard.SetText(string Text);

i had this problem and this worked for me , add this before setting text to clipboard (Clipboard.SetText(string Text))

so it finally become

Clipboard.Clear();
Clipboard.SetText(string Text);
我只土不豪 2024-11-09 17:04:07

我使用了 System.Windows.Forms.Control.WndProc 方法和 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);
}

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);
}
凹づ凸ル 2024-11-09 17:04:07

我也遇到了这个问题,并使用此代码作为 WireGuy 回答。但此代码在我的电脑中出现异常“请求的剪贴板操作未成功”。我把这一行放在 Try Catch 语句中

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

            }

并且工作正常。

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.

ゞ记忆︶ㄣ 2024-11-09 17:04:07

尝试在 while 循环中使用 Try catch 运行 GetDataObject。最终它会成功。

    while (tempObj == null)
    {// get from the clipboard
        try
        {
            tempObj = Clipboard.GetDataObject();
        }
        catch (Exception excep)
        {

        }
    }

Try running GetDataObject in while loop with Try catch. Eventually it will succeed.

    while (tempObj == null)
    {// get from the clipboard
        try
        {
            tempObj = Clipboard.GetDataObject();
        }
        catch (Exception excep)
        {

        }
    }
奢欲 2024-11-09 17:04:07

就我自己而言,我发现剪贴板仍在处理我的请求,而我
正在放一个新的。
<代码>
SendKeys.SendWait("^c");
Clipboard.GetText();

所以我添加了 Sleep,现在效果很好。
<代码>
SendKeys.SendWait("^c");
线程.睡眠(250);
Clipboard.GetText();

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();

黄昏下泛黄的笔记 2024-11-09 17:04:07

由于某种原因,我每次运行时都会遇到“请求的剪贴板操作未成功”异常,

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

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

每次都有效。

然而,有趣的是,

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

两个 SetDataObject 调用都会失败,

我确信它是短暂的错误,就像我前几天设置剪贴板内容时没有问题一样。

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.

朱染 2024-11-09 17:04:07

我在从 Microsoft SQL Server Management Studio 复制数据时突然出现此错误,从那以后我无法复制任何内容。
重新启动 explorer.exe 进程解决了该问题。我猜想资源管理器进程处理大部分剪贴板操作。

I suddenly had this error when copying data from Microsoft SQL Server Management Studio, since then I couldn't copy anything.
Restarting explorer.exe process solved the issue. I guess the explorer process handles most of the clipboard action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文