我正在使用以下代码将文本复制到剪贴板.
System.Windows.Forms.SendKeys.SendWait("^c");
然后我用
Clipboard.GetText()
从剪贴板中获取文本.它运行正常,但是当我在循环中使用剪贴板时它看起来像是在延迟,我得到的内容应该被下一个复制的文本覆盖.如果我把Thread.sleep,它工作正常.我怎么能快速复制并从一个循环中获取正确的内容,没有延迟?
解决方法:
这似乎是一个记录在案的问题. MSDN承认“时间问题”,但不包括完全绕过它们的方法,尽管似乎有一种“更新”的方法,您需要告诉您的程序默认使用.这是文档的一部分:
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>
我在另一个公告板上找到了similar (old) issue,但不幸的是他们的修复与你的相同 – 在访问剪贴板之前延迟了几分之一秒.我找不到任何其他问题的解决方法.考虑到有一个Send和一个SendWait,期望后者在发送后实际等待似乎并不太多!