views:

898

answers:

4

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.

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?

A: 

There is no way to check for something and then depending on the result do something else with the expectation that it could not fail, because unless the check and the action are one atomic operation there is always the possibility that another process or thread does the same in parallel.

This holds whether you try to open the clipboard, open a file, create or delete a directory - you should simply try to do it, maybe several times in a loop, and gracefully handle errors.

mghie
A: 

Try to check GetClipboardOwner, if it's not null and not your Application.Handle, you cannot Open to modify it's content.
And even it seems good to go, it might not be anymore when you actually do it.
So add a try except in a loop until you get it or give up nicely (notifying the user for instance).

François
+2  A: 

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.

You have two possibilities here:

  1. Don't use the Delphi clipboard class. Instead use raw API functions, where you have a little more fine-grained control over possible error situations.
  2. Expect your code to fail by adding an exception handler. Then add some retry code, i.e. retry to set the text three times, perhaps with exponential backoff, before throwing your own error.

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;
DR
This is not a Win32 problem either - it's a simple fact when programming for concurrent systems.
mghie
A: 

i had this same problem frequently and i solved it by inserting a sleep(100) between the copy and the paste. it always worked.

luc
The question doesn't deal with copy and paste. Besides, could you explain why this works? And will it always work? Else it's just trial and error, not solving the problem.
mghie