Problem with methods in a race GUI

This is a discussion on Problem with methods in a race GUI within the C# Programming forums, part of the General Programming Boards category; I am writing a simple program to bet on a race by making simple animations with pictures as the racers. ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    55

    Problem with methods in a race GUI

    I am writing a simple program to bet on a race by making simple animations with pictures as the racers. My problem is that I don't know how to make a button_click event wait for a timer_tick event. I would rather make it wait before continuing so the user won't accidentally mess up the racing/betting progress. Here is the code with the problem in it.

    Code:
            private void StartRaceButton_Click(object sender, EventArgs e)
            {
                this.Enabled = false;
                timer1.Enabled = true;
                timer1.Start();
                // I need it to wait for the timer to finish here.
                this.Enabled = true;
                ChrisRadioButton.Enabled = true;
                GuestRadioButton.Enabled = true;
                Guests2RadioButton.Enabled = true;
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                mustaches[0] = racer1;
                mustaches[1] = racer2;
                mustaches[2] = racer3;
                mustaches[3] = racer4;
                for ( int r = 0; r <= 3; r++)
                {
                    bool IsWinner = mustaches[r].Run();
                    if (IsWinner)
                    {
                        mustaches[r].Winner = true;
                        timer1.Stop();
                        timer1.Enabled = false;
                    }
                }
            }

  2. #2
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    Start a new thread for the timer which keeps track of a variable that is set to a value when it reaches the end of the timer.

    At the end of the timer set the value to the right value, something like:
    Code:
    private void timer1_Tick(object sender, EventArgs e)
    {
        finished = false;
    
      //do your stuff
    
      finished = true;
    }
    And to keep track of the variable:
    Code:
    while (!finished) System.Threading.Thread.Sleep(100);

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by apsync View Post
    And to keep track of the variable:
    Code:
    while (!finished) System.Threading.Thread.Sleep(100);
    It is one of the worst possible solutions... leading to a deadlock, because the Timer component does not start a new thread.

    Instead of waiting for a timer, do the finalisation in the timer's event:

    Code:
    private void StartRaceButton_Click(object sender, EventArgs e)
    {
        this.Enabled = false;
        timer1.Enabled = true;
        timer1.Start();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        mustaches[0] = racer1;
        mustaches[1] = racer2;
        mustaches[2] = racer3;
        mustaches[3] = racer4;
        for ( int r = 0; r <= 3; r++)
        {
            bool IsWinner = mustaches[r].Run();
            if (IsWinner)
            {
                mustaches[r].Winner = true;
                timer1.Stop();
                timer1.Enabled = false;
    
                // Finalise the operation.
                StartRaceButton.Enabled = true;
                ChrisRadioButton.Enabled = true;
                GuestRadioButton.Enabled = true;
                Guests2RadioButton.Enabled = true;
            }
        }
    }
    I never put signature, but I decided to make an exception.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Agree with kmdv - the whole idea of event-driven programming is that the individual event handlers should return quickly so that control goes back to the message pump. No event handler should ever pause for any appreciable length of time, and certainly not while waiting for another event, which won't even be able to occur.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    69
    On the button click event you could unassign the event handler to the button itself, and have it reassigned in the timer_tick method when IsWinner evaluates as true in that if statement. Only if this is necessary though, otherwise you could just disable the button itself (and of course enable it when the task is finished). What could also be done is that the button is assigned to a different method to indicate that progress is still active if the user tries to click the button a second time before the task completes. Lots of things you could do, but avoid the first suggestion as mentioned by others.

    Instead of doing this though, if you're only going to use it once, why keep it in a boolean variable?
    Code:
    bool IsWinner = mustaches[r].Run();
    if (IsWinner)
    {
    Just do this:
    Code:
    if (mustaches[r].Run())
    {
    Because it looks like you're only going to use it once. The only significant part is r, being the incremented numeric value. *NOTE: If this is a readability thing.. Just use a comment to indicate that it means there is a winner, that's what they were invented for.
    Last edited by cstryx; 05-06-2013 at 01:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem With Virtual Methods
    By frassunit in forum C++ Programming
    Replies: 7
    Last Post: 03-23-2009, 07:55 PM
  2. Inheritance - Problem calling methods
    By rusty_turkey in forum C++ Programming
    Replies: 5
    Last Post: 05-31-2007, 04:36 AM
  3. problem w/ class methods
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 08-30-2005, 06:41 AM
  4. what race is god?
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 02-22-2004, 04:38 PM
  5. problem: cant acess class methods using vectors
    By justdoit22 in forum C++ Programming
    Replies: 8
    Last Post: 12-30-2003, 06:47 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21