Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Comments
Sergey Alexandrovich Kryukov 17-Jun-13 2:42am    
Why would you ever need to poll the clipboard? It smells a big abuse...
—SA
Richard MacCutchan 17-Jun-13 3:19am    
Are you sure that there is actually something in the ClipBoard when you try to read it?

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900