2016-11-06 57 views
-4

我正在開發一個C#控制檯應用程序,我需要添加一個計時器,該計時器在我開始寫入「開始」時開始,並在通過10分鐘後自動停止。我怎樣才能做到這一點只使用「靜態無效Main()」?C中的10分鐘計時器#

我有這樣的:

using System; 
using System.Timers; 

namespace myScript 
{ 
    class Program 
    { 
     static void Main() 
     { 
      string getInput = Console.ReadLine(); 

      if (getInput == "start") 
      { 
       //start timer 
      } 

      if (//10 minutes have passed) 
      { 
       //do something 
      } 
     } 
    } 
} 

謝謝!

+0

定義*主類* ...並顯示你已經嘗試/擁有。你的問題是這樣的:我需要一個'x'和'''的程序,你能爲我寫出來嗎? – Jim

+0

我需要一些更多的信息才能夠幫助你。打字開始後應該發生什麼?該應用程序阻止?應該可以啓動多個定時器嗎? – Jonas

+0

@Jonas謝謝!我剛剛編輯了這個問題。 – POILOI

回答

-1
using System; 
    using System.Threading; 

static void Main(string[] args) 
      { 
       Console.WriteLine("Please type \"start\" and press ENTER"); 
       while (true) 
       { 
        var userInput = Console.ReadLine(); 

        if (userInput.Equals("start")) 
        { 
         break; 
        } 
        Console.WriteLine("Not correct, please try again"); 
       } 


       var minutes = 10; 
       Console.WriteLine("Going to sleep for " + minutes + " Minutes..."); 
       Thread.Sleep(1000 * minutes * 60); 

       Console.WriteLine("Done..."); 

       Console.ReadLine(); 
      } 
+0

我必須在頂部添加「使用」某些東西嗎? – POILOI

+0

是的,你需要:使用System.Threading; –

+0

非常感謝你 – POILOI

0
//start timer 
//put this into your if statement 
Timer timer = new Timer (1000 * 60 * 10); 
timer.Elapsed += delegate (object sender, EventArgs e) 
{ 
    //do something 
    timer.Stop(); 
    timer.Dispose(); 
}; 
timer.Start(); 

試試這個,使用system.timers不是線程。這應該啓動10分鐘的定時器,它會在操作結束時執行某些操作並進行處理。

+0

非常感謝你 – POILOI