0

I have a windows form with a label, and a system.net timer called aTimer.

The timer is created in an external class like this:

// Create a timer with a 3 minute interval.
common.aTimer = new System.Timers.Timer(3000);

// Hook up the Elapsed event for the timer.
common.aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

// Set the Interval to 3 minutes (180000 milliseconds).
common.aTimer.Interval = 180000; // 3000; //  
common.aTimer.Enabled = true;
mTimerRunning = true;

and in my form I run the code OnTimedEvent to kick off a background worker

I want to show on my label the number of seconds before the next action is taken.

I tried this but it won't compile:

lblStatus.BeginInvoke(new Action(() =>
                    {
        lblStatus.Text = "timer running - Check inbox in " & common.aTimer.Interval - common.aTimer.Elapsed & " seconds!";
                    }));

I have a compile error The event 'System.Timers.Timer.Elapsed' can only appear on the left hand side of += or -=

So what is the correct way to do this?

Ideally every tick of the timer I would update the label to show when the nxt run will happen.

Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148

1 Answers1

1

I guess you're from VB background.

Following is not a valid c# code.

lblStatus.Text = "timer running - Check inbox in " & common.aTimer.Interval - common.aTimer.Elapsed & " seconds!";
  • & is not used for string concatenation in c#, use + operator instead. Or even better.
  • aTimer.Interval is double, aTimer.Elapsed is an event. You can't add it. It doesn't even makes sense.
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189