无法在timerexpired事件调用中获取剪贴板数据 [英] Cannot get clipboard data in the timerexpired event call

查看:132
本文介绍了无法在timerexpired事件调用中获取剪贴板数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要每隔1秒左右访问剪贴板。但问题是,我已将计时器设置为1000毫秒,并且当调用计时器到期事件时,Clipboard.GetImage()没有数据。

但是如果clipboard.getimage不在定时器调用,然后我就可以得到它。

是否有任何原因或是因为我正在使用,system.timers。

请帮助。

Hi,
I need to access the clipboard every 1 sec or so. But the problem is , i have set the timer to 1000ms, and when the timer expired event is called, the Clipboard.GetImage() has no data.
But in case the clipboard.getimage is out of the timer call, then i am able to get it.
Is there any reason for this or is it because i am using, system.timers.
Please help.

推荐答案

我创建了一个测试程序,从中我发现你可能遇到了跨线程的数据访问问题。



在我的测试程序中,我创建了一个带有图像的图片框。我有按钮将图像复制到剪贴板并清除剪贴板,这样我就可以看到一些动作。



我在表格中添加了 System.Windows.Forms.Timer (从表单设计器工具箱拖到表单上) - 并在其事件处理程序中调用 Clipboard.GetImage()并将结果报告给输出窗口(使用 Debug 对象)。



我还添加了一个不同的计时器 - System.Timers.Timer - 这与第一个计时器完全相同。此外,它使用 Form.Invoke()在前台线程上调用另一个方法 并再次访问剪贴板。



我的结果?

Forms计时器工作正常。 Invoke-to-get-the-data工作正常,没有调用的Timers计时器无法获取数据。



注意 - 还有另一个计时器 - System.Threading.Timer 。我没有测试那个。请注意,它的文档说不建议与Windows窗体一起使用(即它具有 System.Timers.Timer 展示的相同问题);文档不建议在Windows窗体应用程序中使用定时器计时器。
I created a test program, from which I discovered that you're probably encountering a cross-thread data access problem.

In my test program I created a form with a picture box loaded with an image. I had buttons to copy the image to the clipboard and to clear the clipboard so I could see a little action.

I added to the form a System.Windows.Forms.Timer (drag from the form designer toolbox onto the form) - and in its event handler I called Clipboard.GetImage() and reported the results to the output window (using the Debug object).

I also added a different timer - System.Timers.Timer - which did exactly the same as the first timer. In addition, it uses Form.Invoke() to call a different method on the foreground thread and again access the clipboard.

My results?
The Forms timer worked fine. The Invoke-to-get-the-data worked fine, the Timers timer without the invoke failed to get the data.

Note - there is another timer - System.Threading.Timer. I didn't test that one. Note that it's documentation says that it is not recommended for use with Windows Forms (i.e. it has the same "problem" that System.Timers.Timer exhibits); the documentation does not recommend using the Timers timer in a Windows form application either.


这篇关于无法在timerexpired事件调用中获取剪贴板数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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