嵌套的计时器代码不起作用

时间:2017-12-25 19:05:45

标签: c# timer ienumerator

我制作了一个流量机器人...它适用于一个网站。我把它设置到多个网站。 因此我编码如下。

 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('\n');

        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('\n');
        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获取一个代理---&gt;为该代理加载2个网站

请告诉我我的错误

1 个答案:

答案 0 :(得分:0)

我无法对其进行测试,但我认为您的计时器彼此重叠,因此您可以尝试更好地同步他们的计时器

IEnumerator<string> proxies;
IEnumerator<string> websites;
private void button1_Click(object sender, EventArgs e)
{
    List<string> strings = new List<string>();
    strings = textBox2.Text.Split('\n').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('\n').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();
}