Skello
Skello

Reputation: 383

C# Timer Skips Code

I tell the Timer to start in the constructor. It starts, but when it reaches its Timer.Elapsed event it only runs the first if statement in the method. I've checked to see if isWatching is true, and it is, but it still skips it entirely. It doesn't even reach the if(isWatching) line.

Code:

MainWindow.xaml.cs:

public partial class MainWindow : Window
{   
    public SessionManager SM { get; private set; }

    public MainWindow()
    {
        SM = new SessionManager();
        SM.NewDayEvent += SplitSession;
        ///code
    }
}

SessionManager.cs (Some variables have been omitted from this post):

public class SessionManager : INotifyPropertyChanged
{
    public delegate void NewDayEventHandler(object sender, EventArgs ea);
    public event NewDayEventHandler NewDayEvent;

    private bool _isWatching;
    private Timer _timer;
    private bool isWatching
    {
        get
        {
            return _isWatching;
        }
        set
        {
            _isWatching = value;
            if (!_isWatching)
            {
                _clockWatch.Stop();
            }
            else
            {
                _clockWatch.Start();
            }
        }
    }
    #endregion


    public SessionManager()
    {
        _clockWatch = new Stopwatch();
        _timer = new Timer(1000);
        _timer.Elapsed += timerElapsed;//focus on this here

        _isWatching = false;
        current_time = new DateTime();
        CurrentTime = DateTime.Now;
        _timer.Start();
    }

    public void timerElapsed(object sender, ElapsedEventArgs e)
    {
        CurrentTime = DateTime.Now;
        if (CurrentTime.TimeOfDay == TimeSpan.Parse("9:32 AM") && NewDayEvent != null)
        {
            NewDayEvent(this, new EventArgs());
        }
        if (isWatching)
        {
            if (CurrentSession != null)
            {
                //update the timespent variable of the current timeEntry
                if (CurrentSession.currentTimeEntry != null)
                {
                    CurrentSession.currentTimeEntry.TimeSpent = _clockWatch.Elapsed;
                    calculateTotalTime();

                    CalculateFilteredTimeSpent();
                }
            }
        }
    }
}

Upvotes: 0

Views: 191

Answers (1)

degant
degant

Reputation: 4981

You aren't using the correct format while calling TimeSpan.Parse(). The right way to do what you are trying to is:

TimeSpan.Parse("9:32")

Your current code snippet throws a System.FormatException:

A first chance exception of type 'System.FormatException' occurred in mscorlib.dll

However for what you are trying to achieve, trigger an action once every day at a particular time, the above method might not be the best since the chances of it succeeding are very less. The timer will run every 1000 ms and then return the current time of day which has milliseconds included. So the timer elapsed event could be called at 9:32.0001 and might never pass the condition. A better alternative is probably:

if (CurrentTime.TimeOfDay >= TimeSpan.Parse("9:32") && NewDayEvent != null)

This will trigger more than once after that time has elapsed, so you could add a flag which keeps track of what day the last event was processed.

Alternatively you could also look at ScheduleAction in .NET 4.5 or some of the solutions here.

Upvotes: 3

Related Questions