简体   繁体   中英

TimeSpan.FromSeconds gives error of Specified argument was out of the range of valid values.Parameter name: Interval

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?

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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM