I have a method that utilises SendKeys.Send
, waits for a few seconds using System.Threading.Thread.Sleep
and then runs another method to check the colour of a pixel to see if it has changed. The method then runs again as it is called recursively.
This method needs to be able to run thousands of times before stopping. The Winform's UI seems to stop responding when this is running.
I tried to implement a background worker to take the strain off of the UI. I moved the recursive method's code over to the Do_Work
event and called it with RunWorkerAsync
but it crashed, reporting the following:
An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code
Additional information: SendKeys cannot run inside this application because the application is not handling Windows messages.
What's the best way of moving the code away from the UI? I'm not very familiar with background workers so I may be doing it wrong.