C#电梯系统,暂停

时间:2012-02-07 16:23:53

标签: c# timer pausing-execution

在c#中制作一个简单的电梯系统,让它工作,当你输入楼层号码选择一个楼层时,电梯就会到达这个楼层。

下一步是使用按钮选择地板。问题是当电梯到达用户请求的楼层时,程序不接受按钮的输入..

这是我必须调用电梯运动并打开计时器的代码:

private void liftOneMove()
{
    if ((liftOne.Direction == 1) && (lift1.Value <= 40)) // while lifts going up and floor less/equal to 40
    {
        timer1.Enabled = true;
    }
}

这是计时器中的代码:

private void timer1_Tick(object sender, EventArgs e)
{
    lift1.Value++;
    liftOne.CurrentFloor = lift1.Value;
    lblLift1Flr.Text = "" + lift1.Value;
    if (liftOne.FloorRequests.Exists(element => element == lift1.Value))
    {
        RbLift1.Checked = true;
        lblLift1Current.Text = "Doors opening";
        //Need to pause the timer here to allow user to select a floor
        liftOne.FloorRequests.Remove(lift1.Value);

        if(liftOne.FloorRequests.Count == 0)
        {
            liftOne.Direction = 2;
            timer1.Enabled = false;
            MessageBox.Show("Floor Requests  " + liftOne.FloorRequests.Count);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

至于原型,您可能想要做这样的事情以允许用户选择另一个楼层:

RbLift1.Checked = true;
lblLift1Current.Text = "Doors opening";

timer1.Enabled = false; // stop the first timer after reaching the floor
timer2.Enabled = true; // start a second timer, which will restart the first timer in 5 seconds

liftOne.FloorRequests.Remove(lift1.Value);

并使用另一个计时器在5秒内启用第一个计时器:

private void timer2_Tick(object sender, EventArgs e)
{
    timer1.Enabled = true; // restart the first timer
    timer2.Enabled = false; // stop the second timer
}

在这5秒钟期间,用户可以选择另一个楼层。