2017-03-27 146 views
0

我一直在尝试将timer添加到我的代码中,并且使用以下timespan(小时,分钟,秒)。但不明白为什么我保持以下错误:不能隐式地将类型'system.timespan'转换为双倍

Cannot implicitly convert type 'System.timespan' to double.

这是我的代码。

public static void Main(string[] args) 
{ 
    System.Timers.Timer MyTimer = new System.Timers.Timer(); 
    MyTimer.Elapsed += new ElapsedEventHandler(onTimedEvent); 
    MyTimer.Interval = new TimeSpan(0,0,5000); 
    MyTimer.Enabled = true; 
} 

上面将运行而没有任何问题,如果我设置Mytimer间隔为单个值如下。

Mytimer.Interval = 5000; 

回答

7

您必须匹配的类型和Interval是毫秒(double),而不是一个TimeSpan。所以,你可以这样做:

MyTimer.Interval = new TimeSpan(0,0,5000).TotalMilliseconds; 

或者

MyTimer.Interval = 5000; 
// assuming you meant 5000 milliseconds above and not 5000 seconds 
// if not multiple by 1000 for seconds, again by 60 for minutes, again by 60 for hours, etc 
+1

感谢@Igor现在做工精细的财产。 – JNW

0

间隔是双不是一个时间跨度。

0

在计时器在这样的方式定义的时间间隔,我们可以指定双重价值

[TimersDescriptionAttribute("TimerInterval")] 
[SettingsBindableAttribute(true)] 
public double Interval { get; set; } 
相关问题