put in and get out of clipboard in a loop without delays

青春壹個敷衍的年華 提交于 2020-01-02 05:22:11

问题


I'm using the following code to copy text to Clipboard.

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

Then I use

Clipboard.GetText()

to get the text from Clipboard. It works fine, but it looks like it's delaying when I work with clipboard in a loop and I get content that should be overwritten with next copied text. If I put the Thread.sleep, it works fine. How could I fast copy and get the right content from Clipboard in a loop without delay?


回答1:


This appears to be a documented issue. MSDN acknowledges "timing issues" but doesn't include a way to completely get around them, although there does appear to be a "newer" method that you need to tell your program to use by default. Here's a portion of the documentation:

The SendKeys class has been updated for the .NET Framework 3.0. The SendKeys class is susceptible to timing issues, which some developers have had to work around. The updated implementation is still susceptible to timing issues, but is slightly faster and may require changes to the workarounds. The SendKeys class tries to use the previous implementation first, and if that fails, uses the new implementation. As a result, the SendKeys class may behave differently on different operating systems. Additionally, 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 consistent behavior regardless of the operating system, you can force the SendKeys class to use the new implementation by adding the following application setting to your app.config file.

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

I found a similar (old) issue on another bulletin board, but unfortunately their fix was the same as yours - to delay for a fraction of a second before accessing the clipboard. I couldn't find any other workarounds for the issue. Considering there's a Send and a SendWait, it doesn't seem too much to expect the latter to actually wait after the send! :)




回答2:


You definitely can NOT update the clipboard in a loop and expect the data to be available (and accessible to your app) immediately. The application that you're sending the keystroke to is running in its own process, and windows is multi-processing, multi-threading, etc.. So you're looking for the clipboard to be updated, before the other app has gotten a chance to copy it.
Furthermore, since there can be other programs running on the system, monitoring the clipboard for updates (clipboard viewers), you are going to be colliding with those programs when you attempt to get the data from the clipboard.
I don't know why you're trying to do what you're doing, but you should be aware that it's not going to work all the time. You may be able to get it to work in some cases, but not all cases. Unless this is an educational exercise for your own use, you should abandon this approach.

And please read this quote on the subject:

"Programs should not transfer data into our out of the clipboard without an explicit instruction from the user.”
— Charles Petzold, Programming Windows 3.1, Microsoft Press, 1992



来源:https://stackoverflow.com/questions/15441188/put-in-and-get-out-of-clipboard-in-a-loop-without-delays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!