Search code examples
c#datetimetimerdatetimepickerdispatcher

Stop DispatcherTimer when Method has started


I'm still learning C# and I need some help here. I have a dispatchtimer that checks every second if datetime has initialized. It works but the problem is that it continues to do same thing until datetime is over. For example I want datetime to start NotePad. The problem is that dispatchtimer looks for datetime every 1 second which means that notepad starts everyone second. How can I stop the DispatchTimer when DateTime has started it methods?

Here is code:

    private void Start_Click(object sender, RoutedEventArgs e) //Button start.
    {
        test();
    }
    private void startjob() //DateTime checking for DateValue and start's if correct value.
    {
        DateTime? start = DateTimePicker1.Value;
        DateTime? end = DateTimePicker2.Value;
        DateTime now = DateTime.Now;

        if (start == null || end == null)
        {
            Xceed.Wpf.Toolkit.MessageBox.Show("one of the pickers is empty");
        }
        else if (now >= start.Value && now <= end.Value)
        {
            Process notePad = new Process();

            notePad.StartInfo.FileName = "notepad.exe";

            notePad.Start();

        }
    }
    private void test() // DispatchTimer
    {
        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
        dispatcherTimer.Start();
    }


        private void dispatcherTimer_Tick(object sender, EventArgs e)
{           
    try
    {
        startjob();
    }
    catch (Exception)
    {

    } 

Solution

  • You can have the delegated event handler stop the timer.

    First a quick tweak to startJob to notify if it starts up notepad (I'm assuming if it doesn't, you want to continue trying):

    private bool startjob() //DateTime checking for DateValue and start's if correct value.
    {
        DateTime? start = DateTimePicker1.Value;
        DateTime? end = DateTimePicker2.Value;
        DateTime now = DateTime.Now;
    
        if (start == null || end == null)
        {
            Xceed.Wpf.Toolkit.MessageBox.Show("one of the pickers is empty");
        }
        else if (now >= start.Value && now <= end.Value)
        {
            Process notePad = new Process();
    
            notePad.StartInfo.FileName = "notepad.exe";
    
            notePad.Start();
            return true;
        }
        return false;
    }
    

    You didn't post your dispatcherTimer_Tick method, but you should be able to incorporate this as the sender in the event handler is the timer itself:

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        if (startJob())
        {
            //if notepad was started, stop the timer
            DispatcherTimer timer = (DispatcherTimer)sender;
            timer.Stop();
        }
    }
    

    EDIT: If your intent was to stop the timer regardless of whether or not the input was valid to start up notepad, then you can ditch the bool result/check and just call Stop() on the timer in the Tick event.