简体   繁体   English

TimeSpan.FromSeconds 给出指定参数的错误超出有效值范围。参数名称:间隔

[英]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.我在 WPF 应用程序中使用TimeSpan

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我读过这个: 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.您可以在 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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指定的参数超出了有效值的范围。参数名称:值 - Specified argument was out of the range of valid values.Parameter name: value 将新项目添加到ObservableCollection时,“指定的参数超出有效值范围。参数名称:索引” <T> - “Specified argument was out of the range of valid values.Parameter name: index” when adding new item to ObservableCollection<T> 指定的参数超出了有效值的范围。 参数名称:值 - Specified argument was out of the range of valid values. Parameter name: value 指定的参数超出有效值范围。 参数名称:大小 - Specified argument was out of the range of valid values. Parameter name: size 得到“指定的参数超出有效值范围。 参数名称:” - Getting “Specified argument was out of the range of valid values. Parameter name:” 指定的参数超出有效值范围。 参数名称:site - Specified argument was out of the range of valid values. Parameter name: site 错误指定的参数超出了有效值的范围。 参数名称:DataGridview Row Data Bound 中的索引 - Error Specified argument was out of the range of valid values. Parameter name: index in DataGridview Row Data Bound 指定的参数超出了有效值的范围。参数名称:site - 由于创建者更新而导致的错误 - Specified argument was out of the range of valid values. Parameter name: site - Error Due To Creators Update Timespan(0,0,secs)或Timespan.FromSeconds(secs) - Timespan(0,0,secs) or Timespan.FromSeconds(secs) Linq to Object给出“指定的参数超出有效值范围。” - Linq to Object gives “Specified argument was out of the range of valid values.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM