-2

I'm using TimeSpan in my WPF application.

var trigger = new TimeTrigger();
trigger.Repetition.Interval = TimeSpan.FromSeconds(3.0);

It's give me an error:

Specified argument was out of the range of valid values.Parameter name: Interval

I've read this : https://msdn.microsoft.com/en-us/library/system.timespan.fromseconds(v=vs.110).aspx

What I'm doing wrong?

Barry Allen
  • 81
  • 2
  • 14
  • 1
    Apparently the Interval property of the Repetition of your TimeTrigger is throwing that exception, not FromSeconds. – Clemens Sep 27 '16 at 10:42
  • It seems that the `Interval` property expects a String value, according to [this](https://msdn.microsoft.com/en-us/library/windows/desktop/aa382119(v=vs.85).aspx) – Pikoh Sep 27 '16 at 10:44
  • See [RepetitionPattern.Interval](https://msdn.microsoft.com/en-us/library/windows/desktop/aa382119(v=vs.85).aspx): "...the minimum time allowed is 1 minute." – Clemens Sep 27 '16 at 10:46
  • @Pikoh, No it's expect `TimeSpan` – Barry Allen Sep 27 '16 at 10:53
  • Anyway, @Clemens is right, the miminum time is 1 minute – Pikoh Sep 27 '16 at 10:58
  • Yes,right. but can you please suggest me, alternative way? – Barry Allen Sep 27 '16 at 10:59
  • 1
    An alternative way for what? You have not told us what are you trying to do :) – Pikoh Sep 27 '16 at 11:00
  • Oh yes, you're right. so sorry for that. I want to trigger my method every 3 second after my system start. – Barry Allen Sep 27 '16 at 11:04
  • Then you have different timers to use in WPF, as [DispatcherTimer](https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer(v=vs.110).aspx) or [System.Timers.Timer](https://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx) – Pikoh Sep 27 '16 at 11:22

2 Answers2

1

You should use a Timer like shown here: What is the best way to implement a "timer"?

You can call your method in the OnTimerElapsed-Event.

Community
  • 1
  • 1
1

An alternative approach would be:

// set interval of 3 seconds / 3000 msec
System.Timers.Timer timer = new System.Timers.Timer(3000);

bool stopTimer = false;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    // the timer will restart automatically
    timer.AutoReset = true;

    // register the event
    timer.Elapsed += Timer_Elapsed;

    // start the timer
    timer.Start();

}

private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    // execute method here

    // check whether timer can be stopped
    System.Timers.Timer t = sender as System.Timers.Timer;
    if (stopTimer)
    {
        t.AutoReset = false;
    }
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • Thanks for this, but how can use this with `MainWindow()` ? – Barry Allen Sep 27 '16 at 11:31
  • @user6689472 "I want to trigger my method every 3 second after my system start." which method do you want to trigger? I would suggest to use the `Loaded` event to set up and start the timer. I edited my answer. – Mong Zhu Sep 27 '16 at 11:54
  • @user6689472 oh I completely forgot to start the timer.. my mistake. Just add `timer.Start();` as in the edit – Mong Zhu Sep 27 '16 at 16:14