暂停执行而不会冻结表单吗? [英] Pausing execution without the form freezing?
问题描述
以下是我的一种方法中的一些代码:
webBrowser1.Focus(); System.Windows.Forms.SendKeys.Send("{PGDN}");
我正在使用它来强制Web浏览器向下滚动,以便屏幕上显示我正在使用的网站上的图片.
然后我用:
findClick();
我将其编码为:(image1)在屏幕上查找图像并单击.
效果很好.
我的问题是:
(这是下一行代码):
webBrowser1.Focus(); System.Windows.Forms.SendKeys.Send("{PGDN}"); Point one = this.Location; //at this section my program doesn''t wait for the above two commands to run before starting this line //it runs the code before the screen has been scrolled so my picture finder never gets found.
如何在执行下一行命令之前等待一定的时间?
休眠不工作!!!!!!!!!!
该命令将冻结表格,并且不会对此问题执行任何操作.我也不能使用计时器,因为我需要代码保留在此方法中(最好是一行,例如thread.Sleep)
我将如何去做呢?
另一个类似的问题:
using: webBrowser1.Navigate("http://google.com"); second command; third command;
这不等待第一个命令完成,所以我想放置一个计时器,上面写着wait(5000)然后继续.
我知道有关DOCUMENTCOMPLETED事件的信息,我以它为例!
您需要阅读有关线程的知识.等待是没有意义的.发送密钥是一种严重的滥用行为.你为什么要那样做?您需要说明您的目标以获取建议,但是无论您做什么,发送密钥和等待都不是正确的工具.
基本上,您需要处理事件System.Windows.Forms.WebBrowser.DocumentCompleted
,如下所示:
MyBrowser.DocumentCompleted + =(发送方,eventArgs){
(((WebBrowser)sender).Print();
// ...
};
请参阅 http://msdn.microsoft.com/en-us/library /system.windows.forms.webbrowser.documentcompleted.aspx [ ^ ], http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx [
I''m using this to force the webbrowser to scroll down so that a picture on the site I''m using is on screen.
Then I use:
findClick();
I have coded this to mean: using(image1) find image on screen and click.
It works fine.
MY PROBLEM IS:
(here is the next line of code):
webBrowser1.Focus();
System.Windows.Forms.SendKeys.Send("{PGDN}");
Point one = this.Location; //at this section my program doesn''t wait for the above two commands to run before starting this line
//it runs the code before the screen has been scrolled so my picture finder never gets found.
How do I wait for a certain amount of time before executing the next line of command?
THREAD.SLEEP DOESN''T WORK!!!!!!!!!!
This command freezes the form and doesn''t do anything for this problem. I also cannot use timers because I need the code to remain in this method (preferably in one line, like thread.Sleep)
How would I go about doing this?
ANOTHER SIMILAR PROBLEM:
using:
webBrowser1.Navigate("http://google.com");
second command;
third command;
This doesn''t wait for the first command to finish so I would like to place a timer that says wait(5000) then continue.
I KNOW ABOUT DOCUMENTCOMPLETED events, I''m using this as an example!Everything is so wrong, so I was puzzled how to answer.
You need to read about threading. Waiting is pointless. Sending keys is a big abuse. Why would you ever do that? You need to explain your goal to get an advice, but whatever you do, sending keys and waiting will never be a right tool.
Basically, you need to handle event System.Windows.Forms.WebBrowser.DocumentCompleted
, something like this:
MyBrowser.DocumentCompleted += (sender, eventArgs) {
((WebBrowser)sender).Print();
//...
};
See http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted.aspx[^], http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx[^].
这篇关于暂停执行而不会冻结表单吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!