Denver
Denver

Reputation: 147

Timer method wont trigger when inside the method

I have a method called

public void OnCaptured(CaptureResult captureResult){}

Inside of these method i want to call the timer, I tried to enable the timer but it wont trigger at all, i also try to create another method and call it inside the method above to call the timer tick and again doesn't work at all.

here is my timer code:

private void TimeCountDown_Tick(object sender, EventArgs e)
{
    int count = int.Parse(lblCount.Text) -1;
    InvokeCD(count.ToString());
     if (count < 0) {
       TimeCountDown.Enabled = false;
       InvokeCD("5");
     }
}

Upvotes: 1

Views: 579

Answers (1)

NicoRiff
NicoRiff

Reputation: 4883

You have to call Start() method to get the timer working. Setting up the property Enabled to true isn´t enough.

This HAS to work:

System.Timers.Timer timer = new System.Timers.Timer(1000); //it will run every one second

public void OnCaptured(CaptureResult captureResult)
{
     timer.Elapsed += TimeCountDown_Tick;
     timer.Start();
}

private void TimeCountDown_Tick(object sender, System.Timers.ElapsedEventArgs e)
 {
    int count = int.Parse(lblCount.Text) -1;
    InvokeCD(count.ToString());
     if (count < 0) {
       TimeCountDown.Enabled = false;
       InvokeCD("5");
     }
}

Upvotes: 1

Related Questions