1

I'm new to C# and I'm trying to create a simple brain training program using Windows Forms that I learnt when learning Android App development. I'm stuck on the Timer. I have it working and it triggers every second and updates the label on my form. When I add code after I have started the timer, the timer stops. It's like I doesn't run in the background. I've read lots about threading timers and from timers etc, but I've not managed to get anything to work. Like I said, I'm new to C# so please be gentle.... He's my code....

public partial class BrainTrainer : Form
{
    // Set global timer variables & Create Timer
    static int secondCounter = 10;
    static bool play = false;
    static Timer myTimer = new Timer();


    public BrainTrainer()
    {
        // Set up form
        InitializeComponent();
        toggleLabels(false);
        timerLbl.Text = secondCounter.ToString() + "s";


    }

    // Function to loop through labels and disabled them
    private void toggleLabels(bool state)
    {
        var ansLabels = this.Controls.OfType<Label>()
            .Where(c => c.Name.StartsWith("ans"))
            .ToList();

        foreach (var label in ansLabels)
        {

            label.Enabled = state;
        }
    }

    // Event to run every second
    private void TimerEventProcessor(Object myObject, EventArgs e)
    {
        if (secondCounter == 0)
        {
            //Stop Game
            myTimer.Stop();
            play = false;

        }
        else
        {
            // Countdown 1 and update label
            secondCounter--;
            timerLbl.Text = secondCounter.ToString()+"s";
        }
    }

    private void startBtn_Click(object sender, EventArgs e)
    {
        // Hide button, set play to true and enable labels
        startBtn.Hide();
        play = true;
        toggleLabels(true);

        // Set up timer event, interval and start
        myTimer.Tick += new EventHandler(TimerEventProcessor);
        myTimer.Interval = 1000;
        myTimer.Start();
        // Run function to play
        genEquation();    

    }

    private void genEquation()
    {
        while (play)
        {
            Console.WriteLine(secondCounter);   
        }

    }

}

Any help is greatly appreciate, or a link to a helpful tutorial would be great!

13
  • 2
    If you want to alter any UI elements from a timer callback, use the DispatcherTimer class. Commented May 2, 2018 at 11:24
  • a timer is like a metronome on the tick it does something but thats it util the next tick it doesnt do anything. So for example, a timer set to 15 seconds and on ticket to show date/time on a label will update every 15 seconds.. nothing else happens Commented May 2, 2018 at 11:25
  • You are adding event subscription every time the button is clicked, which can cause serious bugs Commented May 2, 2018 at 11:25
  • 2
    You do know that it's not WPF and it is in fact Winforms that you are using Commented May 2, 2018 at 11:43
  • 1
    Just a guess: You are blocking the current Thread and dont give it time to process the .Tick-Event. The Winforms-Timer does not start a thread for it. Try calling Application.DoEvents() in your genEquation() loop to porcess the event. Commented May 2, 2018 at 11:46

1 Answer 1

2

Problem was solved in the Comments

You are blocking the current Thread and dont give it time to process the .Tick-Event. The Winforms-Timer does not start a thread for it. Try calling Application.DoEvents() in your genEquation() loop to porcess the event.

private void genEquation()
{
    while (play)
    {
        Application.DoEvents();   
        Console.WriteLine(secondCounter);
    }    
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.