使用Timer或Interval重复webBrowser1.Navigate?

时间:2013-12-08 00:55:27

标签: c#

我无法弄清楚哪个是重复搜索bing的最佳方法。

必须延迟3-4秒并重复。这是我到目前为止所做的。

var lines = File.ReadAllLines(@"C:\dictionary.txt");
var r = new Random();
var randomLineNumber = r.Next(0, lines.Length - 1);
var line = lines[randomLineNumber];
//Repeat # entered in textBox2
webBrowser1.Navigate("http://bing.com/search?q=" + line);

我可以使用计时器,但我需要一种在X搜索后自动停止的方法。

1 个答案:

答案 0 :(得分:1)

这可能是解决方案;

定义像这样的私有整数字段;

private static int counter=0;

在表单上放置计时器,将其 Interval 属性设置为3000-4000毫秒 并双击设计器上的Timer,在tick事件中写入 以下代码;

counter++;
if(counter==5)// Replace 5 with how many times you want to repeat Navigate method.
{
   var lines = File.ReadAllLines(@"C:\dictionary.txt");
   var r = new Random();
   var randomLineNumber = r.Next(0, lines.Length - 1);
   var line = lines[randomLineNumber];
   //Repeat # entered in textBox2
   webBrowser1.Navigate("http://bing.com/search?q=" + line);
}

由于你已经快完成了,你唯一需要做的就是在你想要启动程序的时候调用 timer1.Start(); 。希望这可以解决你的问题。