嵌套的计时器代码不起作用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了嵌套的计时器代码不起作用相关的知识,希望对你有一定的参考价值。
我制作了一个交通机器人...它适用于一个网站。我把它设置到多个网站。因此我编码如下。
IEnumerator<string> proxies;
IEnumerator<string> websites;
private void button1_Click(object sender, EventArgs e)
{
List<string> strings = new List<string>();
string[] ipaddress = textBox2.Text.Split('
');
for(int i=0;i<Convert.ToInt32(ipaddress.Length);i++){
strings.Add(ipaddress[i]);
}
proxies = strings.GetEnumerator();
timer1.Start();
//websites = strings.GetEnumerator();
//timer1.Start();
// websites.MoveNext();
proxies.MoveNext();
}
private void timer1_Tick(object sender, EventArgs e)
{
WinInetInterop.SetConnectionProxy(proxies.Current);
label1.Text = proxies.Current;
string[] website = textBox1.Text.Split('
');
List<string> stringsweb = new List<string>();
for (int j = 0; j < Convert.ToInt32(website.Length); j++)
{
stringsweb.Add(website[j]);
}
websites = stringsweb.GetEnumerator();
timer2.Enabled = true;
//webBrowser1.Navigate(website[0]);
timer1.Enabled = proxies.MoveNext();
}
private void timer2_Tick(object sender, EventArgs e)
{
webBrowser1.Navigate(websites.Current);
label2.Text = websites.Current;
timer2.Enabled = websites.MoveNext();
}
正如你所看到我在timer1_Tick中调用了timer2_Tick方法......但是我的代码无效......
问题是它没有在timer1_tick中调用timer2_Tick方法,所以网站没有加载...
Timer1,Timer2间隔= 10s
我想要做
从textbox2获取一个代理--->为该代理加载2个网站
请告诉我我的错误
答案
我无法测试它,但我想你的计时器彼此重叠,你可以尝试更好地同步它们的刻度
IEnumerator<string> proxies;
IEnumerator<string> websites;
private void button1_Click(object sender, EventArgs e)
{
List<string> strings = new List<string>();
strings = textBox2.Text.Split('
').ToList();
proxies = strings.GetEnumerator();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
// Stop the first timer until the second one completes
timer1.Enabled = false;
WinInetInterop.SetConnectionProxy(proxies.Current);
label1.Text = proxies.Current;
List<string> stringsweb = textBox1.Text.Split('
').ToList();
websites = stringsweb.GetEnumerator();
timer2.Enabled = true;
}
private void timer2_Tick(object sender, EventArgs e)
{
webBrowser1.Navigate(websites.Current);
label2.Text = websites.Current;
// Let's continue with the second timer until completition
timer2.Enabled = websites.MoveNext();
// Reenable the first when the second timer completes
if(!timer2.Enabled)
timer1.Enabled = strings.MoveNext();
}
以上是关于嵌套的计时器代码不起作用的主要内容,如果未能解决你的问题,请参考以下文章