private void btAuto_1_Click(object sender, EventArgs e)
        {
            //auto(time1, btAuto_1, bt1, stationNum1, stationSum1, timer1);
            if (btAuto_1.Text == "自动")
            {
                btAuto_1.Text = "手动";
                //自动模拟车辆运行                while (stationNum1 < stationSum1)
                {
                    Random rd = new Random();
                    //时间为40-100秒之间
                    time1 = rd.Next(5, 10);
                    timer1.Enabled = true;
                    //timer的间隔为1秒
                    timer1.Interval = 1000;                    t.Start();                    ??????????????????????????????????                    bt1_Click(bt1, e);
                }
            }
            else if (btAuto_1.Text == "手动")
            {
                btAuto_1.Text = "自动";
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (time1 > 0)
            {
                this.label_time_1.Text = time1.ToString();
                time1--;
            }
            else
            {
                timer1.Stop();
            }
        }
现在想做的就是:
    执行完timer1.Stop()之后,再执行bt1_Click(),执行之后再重新进入while循环。
    至于StationNum和stationSum就不用管了,click里面会处理的。

解决方案 »

  1.   

    上次那样不行?
    这样呢?bool waitting = false;
    private void btAuto_1_Click(object sender, EventArgs e)
    {
    //auto(time1, btAuto_1, bt1, stationNum1, stationSum1, timer1);
    if (btAuto_1.Text == "自动")
    {
    btAuto_1.Text = "手动";
    //自动模拟车辆运行 while (stationNum1 < stationSum1)
    {
    waitting = true;
    Random rd = new Random();
    //时间为40-100秒之间
    time1 = rd.Next(5, 10);
    timer1.Enabled = true;
    //timer的间隔为1秒
    timer1.Interval = 1000; t.Start(); while(waitting) Application.DoEvents();//暂停 bt1_Click(bt1, e);
    }
    }
    else if (btAuto_1.Text == "手动")
    {
    btAuto_1.Text = "自动";
    }
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
    if (time1 > 0)
    {
    this.label_time_1.Text = time1.ToString();
    time1--;
    }
    else
    {
    timer1.Stop();
    waitting = false;//设置标记
    }
    }
      

  2.   


    谢谢这位仁兄
    这次可以了
    和我想要的结果一样Application.DoEvents();//暂停
    想问下这句是什么意思呢?
      

  3.   

    Application.DoEvents();//暂停
    转移控制权给主窗体,主线程
      

  4.   

    这样的话又出现个问题
    当由“自动”变为“手动”的时候,就不行了,那个while循环停不下来
      

  5.   

    跳不出while循环的问题解决了还有就是,我有几个btAuto_n_Click(),每个里都有while循环,用了Application.DoEvents()之后,其他的timer就会都停掉,只剩一个timer在走。