循环插入和退出剪贴板 [英] put in and get out of clipboard in a loop without delays

查看:76
本文介绍了循环插入和退出剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将文本复制到剪贴板。

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

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

然后我使用

Clipboard.GetText()

从剪贴板获取文本。它可以正常工作,但是当我循环使用剪贴板并收到应该用下一个复制的文本覆盖的内容时,它似乎延迟了。如果我把Thread.sleep放进去,那就可以了。我该如何快速地从剪贴板中快速复制并从剪贴板中获取正确的内容而没有延迟?

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?

推荐答案

您绝对无法更新剪贴板循环运行,并希望数据立即可用(您的应用可以访问)。您要发送击键的应用程序正在其自己的进程中运行,而Windows是多处理,多线程等。因此,您正在寻找要更新的剪贴板,而另一个应用程序尚未获得更新。

此外,由于系统上可能有其他程序正在运行,监视剪贴板中的更新(剪贴板查看器),因此当您尝试获取这些文件时,将与这些程序发生冲突。剪贴板中的数据。

我不知道您为什么要尝试做自己正在做的事情,但是您应该知道它不会一直工作。在某些情况下(但并非在所有情况下),您也许可以使它工作。除非这是您自己的教育用途,否则应该放弃这种方法。

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:

如果没有用户的明确指示,程序不得将数据从剪贴板中传输到我们的剪贴板中。

-Charles Petzold,Windows 3.1编程,Microsoft Press,1992

"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

这篇关于循环插入和退出剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆