2013-12-18 83 views
1

我使用C#創建應用程序,當它們被請求並通過我創建的網站放入數據庫時​​播放某些Youtube視頻。然後,我正在構建的程序將接受請求,並將在Chrome中打開並播放它。視頻完成後,它將終止webbrowser.exe進程。當它結束時,記錄將從數據庫中刪除,因此歌曲不會再播放。而我製作的定時器將重新開始,並將等待5分鐘播放下一首歌曲。這工作正常,但問題是,當數據庫中沒有記錄程序崩潰時,我如何才能重新啓動計時器? 這是我的代碼片段,其中的問題是;var變空時重置計時器

Execute(); 
    InitializeComponent(); 
    var aTimer = new System.Timers.Timer(); 
    aTimer.Elapsed += new ElapsedEventHandler((sender, e) => Execute()); 
    aTimer.Interval = 300000; 
    aTimer.Enabled = true; 
} 
public void Execute() 
{ 
    int Tijd; 
    int Videolengte; 
    string resultaat; 
    string URL = ""; 

    Database db = new Database(CONNECTION_STRING); 
    object[] result = db.GetFirstRecord(); 


     URL = (string)result[1]; 
     Process browser = Process.Start(@"chrome.exe", 
       "http:\\www.youtube.com/watch?v=" + URL); 
     Audio.SetApplicationVolume(APP, 15); 

部分,我需要做一個if語句來檢查var結果是否爲空,但是我可以使它再次從aTimer開始? (對不起,壞的格式,我不能與這個經驗豐富的

+2

請刪除所有不相關的代碼,這是一個混亂。如果不正確,版主會爲你完成格式化 – Abbas

+0

完成,現在看起來好多了 – joostmakaay

+0

只需檢查數據庫是否在'如果是的話,執行()'並返回k再次在下一個時間間隔 –

回答

1

,你可以檢查所有的陣列,樣品:

// check if there is something in the collection 
if (result != null && result.Any() && result.Count >= 1) 
{ 
    URL = result[1] as string; 
    if (!string.IsNullOrEmpty(URL)) 
    { 
     // continue safe ... 
    }  
    else 
    { 
     // stop the timer... or something like this 
     aTimer.Stop(); 
    } 
} 

只是爲了確保,aTimer.Enabled=true是如aTimer.Enable=falseaTimer.Start()相同aTimer.Stop()