Requested Clipboard operation did not succeed的解决办法

简介:

在使用Clipboard.SetText(strTar) 将文本复制进剪贴板的时候,有时候会导致Requested Clipboard operation did not succeed的异常,但有时候又不会。

原因:

       剪切板被所有进程共享,但是不能同时被两个进程修改。所以,如果有两个程序同时访问剪贴板,就有可能导致异常的发生。即使电脑中其他的应用程序的窗口没有处于激活状态,它们也可能在后台访问剪贴板,从未导致剪贴板访问冲突。

解决方法:

        在捕获到异常的时候,间隔一定的时间,再次尝试对剪贴板进行操作。

        在.NET framework (2.0)和其以上的版本已经提供了一个方法Clipboard.SetDataObject(object data, bool copy, int retryTimes, intretryDelay) 用来解决这个问题。他会在访问剪贴板冲突的时候,间隔一定的时间,尝试访问剪贴板。

方法的说明:

复制代码
//
        // 摘要:
        //     尝试指定的次数,以将数据置于系统剪贴板中,且两次尝试之间具有指定的延迟,可以选择在退出应用程序后将数据保留在剪贴板中。
        //
        // 参数:
        //   data:
        //     要置于剪贴板中的数据。
        //
        //   copy:
        //     如果想在退出应用程序后将数据保留在剪贴板中,则为 true;否则为 false。
        //
        //   retryTimes:
        //     尝试将数据置于剪贴板中的次数。
        //
        //   retryDelay:
        //     两次尝试之间暂停的毫秒数。
        //
        // 异常:
        //   System.Threading.ThreadStateException:
        //     当前线程未处于单线程单元 (STA) 模式下。请将 System.STAThreadAttribute 添加到应用程序的 Main 方法中。
        //
        //   System.ArgumentNullException:
        //     data 为 null。
        //
        //   System.ArgumentOutOfRangeException:
        //     retryTimes 小于零。- 或 -retryDelay 小于零。
        //
        //   System.Runtime.InteropServices.ExternalException:
        //     未能将数据置于剪贴板中。这种情况通常发生在剪贴板正在被其他进程使用的时候。
        public static void SetDataObject(object data, bool copy, int retryTimes, int retryDelay);
复制代码

参考链接

ClipBoard.SetText Exception

Requested Clipboard operation did not succeed—MSDB Forums.

Requested Clipboard operation did not succeed—StackOverFlow

本文转自陈哈哈博客园博客,原文链接http://www.cnblogs.com/kissazi2/archive/2013/03/12/2954915.html如需转载请自行联系原作者

kissazi2
相关文章
|
6月前
|
开发者
上传苹果版本时错误解决办法:No suitable application records were found. Verify your bundle identifier
上传苹果版本时错误解决办法:No suitable application records were found. Verify your bundle identifier
55 2
|
6月前
|
API Windows
Qt-解决异常报错“QAxBase::setControl: requested control XXX could not be instantiated”
Qt-解决异常报错“QAxBase::setControl: requested control XXX could not be instantiated”
|
11月前
|
开发者 iOS开发
Xcode运行报错The operation couldn’t be completed. Unable to launch xxx because it has an invalid code...
Xcode运行报错The operation couldn’t be completed. Unable to launch xxx because it has an invalid code...
634 0
解决办法:configure: error: You requested SRTP (requires libsrtp) but not found...die
解决办法:configure: error: You requested SRTP (requires libsrtp) but not found...die
115 0
|
iOS开发
Xcode报错:Failed to find a suitable device for the type SimDeviceType解决方法
Xcode报错:Failed to find a suitable device for the type SimDeviceType解决方法
182 0
|
Android开发 Kotlin
【错误记录】Android Studio 运行报错 ( There is not enough memory to perform the requested operation. )
【错误记录】Android Studio 运行报错 ( There is not enough memory to perform the requested operation. )
567 0
【错误记录】Android Studio 运行报错 ( There is not enough memory to perform the requested operation. )
如何处理错误消息Unable to install breakpoint due to missing line number attributes
如何处理错误消息Unable to install breakpoint due to missing line number attributes
158 0
如何处理错误消息Unable to install breakpoint due to missing line number attributes