2

I have an issue where in i am trying to retrieve object from clipboard modify it and add it back ...

Time and again I keep getting

requested operation on clipboard could not be performed

Looks like the clipboard is being locked by some process, how do I free the clipboard?

sll
  • 61,540
  • 22
  • 104
  • 156
Nick
  • 586
  • 2
  • 7
  • 22
  • Could you please clarify when you get this error message. Is it when you retrieve the object or when you set the modified object back? Do you have a code sample to share for critique? – BenSwayne Nov 25 '11 at 23:46
  • Use Taskmgr.exe, Processes tab and start killing processes one by one until you find the troublemaker. Uninstall it. – Hans Passant Nov 28 '11 at 14:06
  • I did it by starting a timer and retrieving in the timer_tick.Probably the context switch helped to free the clipboard. unsure of the reasons but works like a charm. – Nick Nov 28 '11 at 19:48

1 Answers1

0

You cannot free it. If another app has the clipboard open, you have to wait until it closes it. If the clipboard has been recently (within the past second or two) modified, you should expect that other apps will be opening it for examination, and therefore should expect failures if you expect to be able to open it immediately yourself. You need to use try..except handlers, along with sleep(), and a "3 strikes" loop.
pseudo code:

Success := false;
Attempts := 0;
While (Attempts < 3) and (Success = false) do
begin
  Try
    inc(Attempts);
    OpenClipboard;
    Success := true;
  except
    sleep(attempts * 1000);
  end
end;
Chris Thornton
  • 15,620
  • 5
  • 37
  • 62
  • 1
    I did it by starting a timer and retrieving in the timer_tick.Probably the context switch helped to free the clipboard. unsure of the reasons but works like a charm... – Nick Nov 28 '11 at 19:46
  • Timers will work sometimes. But you cannot account for systems with lots of clipboard monitoring. LOTS of programs monitor the clipboard, some are fairly lightweight, such as Google Earth (looking for URLs?) some can take a long time, such as RemoteDesktop, which must synchronize the local/remote clipboards over the network. You cannot predict the latency, so a timer by itself will not work reliably. You need an except/sleep/retry loop. – Chris Thornton Nov 28 '11 at 19:53