10 Replies - 1316 Views - Last Post: 11 November 2012 - 09:54 AM Rate Topic: -----

#1 Darkstar2500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-November 12

Problem with queuing button events.

Posted 10 November 2012 - 06:04 AM

Hello, I have a problem with a task I have to perform. I'm currently doing a project and it involves an elevator car.When I press a button to request the lift the square goes up and there is a display status window that displays the status of the car e.g. going up and door status. My problem is that if I press a button while it's in motion it'll display things attributed to that button e.g. if I'm on the ground floor and I request the lift while it's going up (mid cycle) the status will say going down while the elevator continues to rise but when it gets to the top floor it will just sit there and not go down. How do I prevent it from displaying anything if another button is pressed while the car is in motion ?

Snippets of my code (I'm using a timer to delay the elevator status)
private void one_Click_1(object sender, EventArgs e)
        {
            
              
            if (atFloor1 == true)
            {
                
                status.Text = "Doors Open";
                one.Enabled = false;
                one.BackColor = Color.Tomato;
                timeDelay(2000, (o, a) => status.Text = "Doors Closed");                
                timeDelay(2700, (o, a) => status.Text = "Floor 1");
                timeDelay(2701, (o, a) => one.Enabled = true);
                timeDelay(2700, (o, a) => textBox1.Text = "1");
                timeDelay(2701, (o, a) => one.UseVisualStyleBackColor = true);
                

            }

            else if(atFloor0 == true)
            {
                status.Text = "Going Up";
                one.Enabled = false;
                one.BackColor = Color.Tomato;
                timeDelay(700,  (o, a) => goingUp.Start());                
                timeDelay(5501, (o, a) => textBox1.Text = "1");
                timeDelay(5600, (o, a) => status.Text = "Doors Open");
                timeDelay(6500, (o, a) => status.Text = "Doors Closed");
                timeDelay(8000,(o, a) => status.Text = "Floor 1");
                timeDelay(8001, (o, a) => one.Enabled = true);
                timeDelay(8002, (o, a) => one.UseVisualStyleBackColor = true);                
                
            }
            
        }



Another button
private void zero_Click_1(object sender, EventArgs e)
        {
            if (atFloor1 == true)
            {
                status.Text = "Going Down";
                zero.Enabled = false;
                zero.BackColor = Color.Tomato;
                timeDelay(700,  (o, a) => goingDown.Start());                
                timeDelay(5501, (o, a) => textBox1.Text = "0");
                timeDelay(5600, (o, a) => status.Text = "Doors Open");
                timeDelay(6500, (o, a) => status.Text = "Doors Closed");
                timeDelay(8000,(o, a) => status.Text = "Floor 0");
                timeDelay(8001, (o, a) => zero.Enabled = true);
                timeDelay(8002, (o, a) => zero.UseVisualStyleBackColor = true);
            }

            else if (atFloor0 == true)
            {
                status.Text = "Doors Open";
                zero.Enabled = false;
                zero.BackColor = Color.Tomato;
                timeDelay(2000, (o, a) => status.Text = "Doors Closed");
                timeDelay(2700, (o, a) => status.Text = "Floor 0");
                timeDelay(2701, (o, a) => zero.Enabled = true);
                timeDelay(2702, (o, a) => zero.UseVisualStyleBackColor = true);
            }
        }




EDIT : I had come up with a solution that disables all the buttons while a process is running but that doesn't sound practical to me.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with queuing button events.

#2 coder3788  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 62
  • Joined: 06-November 12

Re: Problem with queuing button events.

Posted 10 November 2012 - 06:12 AM

View PostDarkstar2500, on 10 November 2012 - 06:04 AM, said:

How do I prevent it from displaying anything if another button is pressed while the car is in motion ?


Add a Boolean property to the form (ex: IsInMotion)
When the motion of the car starts set it to true.
When the motion of the car stops set it to false.

When another button is pressed, if IsInMotion is false,display messages. Else, don't.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Problem with queuing button events.

Posted 10 November 2012 - 07:54 AM

I'd also suggest you stop typing and do some more planning.
Is it really your intention to duplicate all your code 50 time if there are 50 floors, just so you can display the floor number?

This would be a good place to create a method that takes the floor number as a parameter.
Was This Post Helpful? 0
  • +
  • -

#4 Darkstar2500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-November 12

Re: Problem with queuing button events.

Posted 10 November 2012 - 09:11 AM

Quote

I'd also suggest you stop typing and do some more planning.
Is it really your intention to duplicate all your code 50 time if there are 50 floors, just so you can display the floor number?

This would be a good place to create a method that takes the floor number as a parameter.


I appreciate your input, I realised my mistake some time ago during coding but I was far too deep into my program so I decided to keep the code as it was instead of creating a new method and getting rid of all the methods.

This post has been edited by tlhIn`toq: 10 November 2012 - 09:29 AM
Reason for edit:: Fixed quote tag

Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Problem with queuing button events.

Posted 10 November 2012 - 09:45 AM

The time to fix mistakes is when you find them, not at the end of development. It costs more time to fix them later rather than sooner.

There should be one button event and some form of queue. The elevator needs to be smart enough to visit floors that have been pressed as it comes to them, rather than in order.
Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Problem with queuing button events.

Posted 10 November 2012 - 10:23 AM

You also have way too much happening in in the button_Click handler.
A method should have one purpose in life, with no side effects.
But you're doing lots of things in it.
Amongst other things you have the doors opening and closing every time a floor is selected. Have you ever been on an elevator that did that?

OpenDoors()
CloseDoors()
ArriveAtFloor(int FloorNumber)
AddFloorToQueue(int FloorNumber)
enum Directionality
{
    Stopped,
    Up
    Down
}

Was This Post Helpful? 0
  • +
  • -

#7 Darkstar2500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-November 12

Re: Problem with queuing button events.

Posted 10 November 2012 - 11:32 AM

View PosttlhIn`toq, on 10 November 2012 - 10:23 AM, said:

You also have way too much happening in in the button_Click handler.
A method should have one purpose in life, with no side effects.
But you're doing lots of things in it.
Amongst other things you have the doors opening and closing every time a floor is selected. Have you ever been on an elevator that did that?

OpenDoors()
CloseDoors()
ArriveAtFloor(int FloorNumber)
AddFloorToQueue(int FloorNumber)
enum Directionality
{
    Stopped,
    Up
    Down
}



Thank your for the help.

The problem description said the elevator control panel needs to have two buttons and so my control panel has two buttons, when the button for the same floor the user is on is pressed while the user is inside the car the doors will open since I have no open doors button I figured they will just open.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Problem with queuing button events.

Posted 10 November 2012 - 12:03 PM

I want you to stop and think. Two buttons. Have you every been in an elevator with only two buttons? Do you think you are going to get a high grade if you only do the minimum amount mentioned in the instructions? Is it fair to think that in a future chapter they are going to say Take the simple elevator project from chapter 2 and add a control panel inside the elevator so the rider can select any floor G-20.

You don't have to have an OpenDoor button to make use of an OpenDoor() method. When the elevator arrives at a floor it should call the OpenDoor method - Which does whatever is right at that time: ConfirmBrakesAreLocked, DisplayStatus, PlayOpeningDoorTone, AnnounceFloorNumber

This way when you arrive at a floor all you have to do is

void ArrivedAtFloor()
{
   OpenDoors();
   WaitSeconds(10);
   CloseDoors();
}


Is that not a whole lot cleaner? Plus when you update the behavior for the OpenDoors method you don't copy/paste that code in 25 places.
Was This Post Helpful? 0
  • +
  • -

#9 Darkstar2500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-November 12

Re: Problem with queuing button events.

Posted 11 November 2012 - 03:03 AM

View PosttlhIn`toq, on 10 November 2012 - 12:03 PM, said:

I want you to stop and think. Two buttons. Have you every been in an elevator with only two buttons? Do you think you are going to get a high grade if you only do the minimum amount mentioned in the instructions? Is it fair to think that in a future chapter they are going to say Take the simple elevator project from chapter 2 and add a control panel inside the elevator so the rider can select any floor G-20.

You don't have to have an OpenDoor button to make use of an OpenDoor() method. When the elevator arrives at a floor it should call the OpenDoor method - Which does whatever is right at that time: ConfirmBrakesAreLocked, DisplayStatus, PlayOpeningDoorTone, AnnounceFloorNumber

This way when you arrive at a floor all you have to do is

void ArrivedAtFloor()
{
   OpenDoors();
   WaitSeconds(10);
   CloseDoors();
}


Is that not a whole lot cleaner? Plus when you update the behavior for the OpenDoors method you don't copy/paste that code in 25 places.



Yes, it is cleaner and would make commenting much easier, I'm half way through with converting the code to use methods instead of having to copy and paste the same thing over and over.

No, I have not been in an elevator with just two buttons but the task list clearly outlines what needs to be done and I have covered every single one of those tasks except for the queuing problem.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Problem with queuing button events.

Posted 11 November 2012 - 08:21 AM

Well, if you're good with doing the minimal amount of work and happy with getting and average grade then I'm happy for you. Its just not a mindset I'm familiar with. People who just do the minimum don't stay with our company long.
Was This Post Helpful? 0
  • +
  • -

#11 Darkstar2500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-November 12

Re: Problem with queuing button events.

Posted 11 November 2012 - 09:54 AM

View PosttlhIn`toq, on 11 November 2012 - 08:21 AM, said:

Well, if you're good with doing the minimal amount of work and happy with getting and average grade then I'm happy for you. Its just not a mindset I'm familiar with. People who just do the minimum don't stay with our company long.


I'm not happy with doing minimal work, I'm concerned with achieving the overall goal and getting the best grade I can which I will because I've covered every single task there is on the task list. I've implemented sound files to announce the floor dumber and the status of the door when floor is reached, all of which were not outlined in the problem description. If they said make an elevator and didn't put restrictions around it or give us a task list, I would have added more than 2 buttons but they didn't so I did what was required of me.

Thanks for the words of wisdom regarding the methods and event handling.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1