如何修复“无法打开剪贴板:拒绝访问”错误? [英] How can I fix "Cannot open clipboard: Access Denied" errors?

查看:4260
本文介绍了如何修复“无法打开剪贴板:拒绝访问”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将文本复制到剪贴板:

I am using the following code to copy text to the clipboard:

  Clipboard.Open;
  try
    Clipboard.AsText := GenerateClipboardText;
  finally
    Clipboard.Close;
  end;

似乎随机我得到无法打开剪贴板:访问被拒绝的错误。我猜这些错误是由其他应用程序锁定剪贴板引起的,但是我似乎从来没有对其他应用程序做任何事情,这些应用程序应该导致锁定。

Seemingly at random I get "Cannot open clipboard: Access Denied" errors. I'm guessing that these errors are caused by other application locking the clipboard, but I never seem to be doing anything with other applications that should cause the locks.

奇怪的是,我的用户似乎正在报告Vista和Windows 7的错误,而不是XP。

Strangely my users seem to be reporting more of the errors with Vista and Windows 7 than with XP.

有没有办法检查剪贴板在尝试访问之前是否被锁定?

Is there a way to check if the clipboard is locked before trying to access it?

推荐答案

这不是Delphi的问题。因为剪贴板可以随时锁定,即使您检查,如果剪贴板当前未锁定,则可能会在检查后直接锁定。

This is not a Delphi problem. Because the clipboard can be locked any moment, even if you check, if the clipboard is currently not locked, it might become locked directly after the check.

您有两种可能性:


  1. 不要使用Delphi剪贴簿类。而是使用原始API函数,您可以对可能的错误情况进行更细致的控制。

  2. 通过添加异常处理程序来预期您的代码失败。然后添加一些重试代码,即重试以设置文本三次,也许是指数退避,然后再抛出自己的错误。

I '推荐第二个解决方案,因为它将是更像Delphi的方法,最终将导致更清晰的代码。

I'd recommend the second solution, because it'd be the more Delphi-like approach and in the end will result in cleaner code.

while not Success do
try
  //Set the clipboard
  Success := True;
except
  on Exception do
  begin
    Inc(RetryCount);
    if RetryCount < 3 then 
      Sleep(RetryCount * 100)
    else 
      raise MyException.Create('Cannot set clipboard');
  end;
end;

这篇关于如何修复“无法打开剪贴板:拒绝访问”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆