1

Making a simple elevator system in c#, got it working for when yuo select a floor by typing in a floor number, the lift then goes to this floor.

The next stage is to select a floor using a button. The problem is when the lift gets to the floor that the user requested, the program does not accept the input from a button..

This is the code i have to call the lift movement and switch the timer on:

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

This is the code within the timer:

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);
        }
    }
}
Scott Smith
  • 1,823
  • 17
  • 15
  • One issue I see - you are accessing a UI controls like textboxes, buttons in the Timer thread, this could cause some side effects. You should use special techniques to dispatch such changes to the UI thread, depends on which framework you are using either WPF/WinForms – sll Feb 07 '12 at 16:27
  • Its only a prototype atthe minute, we'll worry about that kind of stuff after, we just need it to pause before the last IF statement whilst we take an input from the user. – Joel The Man The Programmer Feb 07 '12 at 16:31
  • can you add timer1.Enabled = false; to the move method and check a state or boolean if boolean timer on else timer off..? its the ontimer event that you want to control from what I am seeing.. – MethodMan Feb 07 '12 at 16:43

1 Answers1

0

As for the prototype, you may want to do something like this to allow the user to select another floor:

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);

and use another timer to enable the first timer in 5 seconds:

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

During this 5-second period, user can choose another floor.

B Faley
  • 17,120
  • 43
  • 133
  • 223