我有一种方法利用SendKeys.Send
,等待几秒钟,使用System.Threading.Thread.Sleep
,然后运行另一种方法来检查像素的颜色以查看它是否已更改。然后该方法再次运行,因为它是递归调用的。递归方法在UI上施加应变
此方法需要能够在停止前运行数千次。运行时,Winform的用户界面似乎停止响应。
我试图实现一个后台工作来消除UI的压力。我搬到了递归方法的代码移交给Do_Work
事件,并与RunWorkerAsync
叫,但它坠毁,报告如下:
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.
什么是从UI移动码远的最佳方式?我对背景工作人员不是很熟悉,所以我可能做错了。
你可以使用后台工作者,只需使用'Invoke(new Action(SendKeys.Send),keys)'在UI线程上分派调用。 –
Lou
BGW不会帮你在这里。 BGW存在移动长时间运行,** CPU绑定**,*非UI *任务到另一个线程。你有一个长期的* IO绑定*,* UI *操作在你的手中。 – Servy