2017-03-27 152 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; } 
相關問題