Paste and exit clipboard in a loop without delay

I use the following code to copy text to the clipboard.

System.Windows.Forms.SendKeys.SendWait("^c"); 

Then i use

 Clipboard.GetText() 

to get text from the clipboard. It works fine, but it looks like it is delayed when I work with the clipboard in a loop, and I get content that should be overwritten with the next copied text. If I put Thread.sleep it works fine. How can I quickly copy and get the correct content from the clipboard in a loop without delay?

+4
source share
2 answers

This is apparently a documented issue. MSDN confirms "synchronization problems", but does not include a way around them completely, although it seems to be a "new" method that you need to tell your program to use by default. Here is the piece of documentation:

SendKeys class updated for .NET Framework 3.0. The SendKeys class is susceptible to time issues that some developers had to work on. The updated implementation is still prone to synchronization issues, but a little faster and may require a workaround change. The SendKeys class first tries to use the previous implementation, and if that fails, the new implementation is used. As a result, the SendKeys class can behave differently on different operating systems. In addition, when the SendKeys class uses the new implementation, the SendWait method will not wait for messages to be processed when they are sent to another process. If your application relies on constant behavior regardless of the operating system, you can force the SendKeys class to use the new implementation by adding the following application parameter to your app.config file.

 <appSettings> <add key="SendKeys" value="SendInput"/> </appSettings> 

I found a similar (old) problem on another bulletin board, but, unfortunately, their correction was the same as yours - for a delay of a fraction of a second before accessing the buffer. I could not find any other workarounds for this problem. Given that there is Send and a SendWait , it seems there is not too much to expect that the latter is really waiting after sending! :)

+3
source

You definitely can’t update the clipboard in a loop and expect the data to be available (and available for your application) immediately. The application that you send the keystroke runs in its own process, and the windows are multi-processor, multi-thread, etc. Therefore, you are looking for an updated clipboard before another application gets a chance to copy it.
In addition, since the system may have other programs that control the clipboard for updates (clipboard), you will encounter these programs when trying to get data from the clipboard.
I don’t know why you are trying to do what you are doing, but you must know that it will not work all the time. You may be able to make it work in some cases, but not in all cases. If this is not a training exercise for your own use, you should abandon this approach.

And please read this quote on the topic:

"Programs should not transfer data to ours from the clipboard without explicit instructions from the user."
- Charles Petzold, Windows 3.1 Programming, Microsoft Press, 1992

+3
source

All Articles