2015-11-04 45 views
0

我有一個關於計時器的簡短問題。在我的代碼中,我想創建一個種植農場的遊戲,並帶有一個顯示工廠是否完成的計時器。
爲什麼這樣的:C#Timer-Crop農場遊戲錯誤

public static string currentPlant; 
    public static Timer growTimer; 
    public static void InitGrowTimer(int time, string name) 
    { 
     growTimer = new Timer(); 
     growTimer.Tick += new EventHandler(growTimer_Finished); 
     growTimer.Interval = time; 

     currentPlant = name; 
    } 
    public static void plantCrop(string crop) 
    { 
     if (plantActive == false) 
     { 
      if (plants.Contains(crop.ToLower())) 
      { 

       // growTimer.Interval = <plant>Time; 
       // proceed plants 
       switch (crop.ToLower()) 
       { 
        case "wheat": 
         InitGrowTimer(wheatTime, wheatName); 
         growTimer.Start(); 
         break; 

        default: 
         MessageBox.Show("FATAL ERROR\nThe plant is contained in the definition list but not in the plant menu!", "Civilisation", MessageBoxButtons.OK, MessageBoxIcon.Error); 
         break; 
       } 

      } 
      else 
      { 
       MessageBox.Show("This plant is not available!", "Civilisation", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
     } 
     else 
     { 
      Console.ForegroundColor = ConsoleColor.Red; 
      Console.WriteLine("There is already a plant in progress! Current plant: {0}", currentPlant); 
     } 
    } 

    private static void growTimer_Finished (object sender, EventArgs e) 
    { 
     growTimer.Stop(); 
     MessageBox.Show("Your " + currentPlant + " is finished!", "Civilisation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 
    } 

不啓動定時器,或只是不顯示在最後的消息框。我在創建計時器或創建滴答事件時做錯了什麼?

編輯:這是我的主要方法:

static void Main(string[] args) 
    { 
     InitializeLists(); 
     // game begin 
     Farm.plantCrop("wheat"); 
     Console.ForegroundColor = ConsoleColor.Green; 
     Console.Write("Please enter your desired name: "); 
     QC.resetColors(); 
     name = Console.ReadLine(); 
     Console.WriteLine(/*Introduction*/"Welcome to the world of Civilisation. In this world it is your choice what\n" + 
              "you are up to. You can be a farmer, miner or fighter, whatever you want, the\n" + 
              "world is yours to explore! Have fun!" 
             ); 
     Console.ReadKey(); 
     Console.Clear(); 

     while (true) // run game 
     { 
      // menu 
      Console.Write("   What do you want to do?\n" + 
          "Farm Mine Explore Go to the city\n" 
         ); 
      input = Console.ReadLine(); 
      if (menuContent.Contains(input.ToLower())) 
      { 
       if (input.ToLower() == menuContent.ElementAt(0)) 
       { 
        // farm 
        Console.ForegroundColor = ConsoleColor.Green; 
        Console.WriteLine("-- Farm --\nSelect a crop to plant:"); 
        Console.ForegroundColor = ConsoleColor.DarkGreen; 
        int icount = 0; 
        for (int i = 0; i < Farm.plants.Count; i++) 
        { 
         if (icount < 3) 
         { 
          Console.Write(Farm.plants.ElementAt(i)); 
          Console.Write("\t\t"); 
          icount++; 
         } 
         else 
         { 
          Console.Write("\n"); 
          icount = 0; 
          Console.Write(Farm.plants.ElementAt(i)); 
          Console.Write("\t\t"); 
          icount++; 
         } 
        } 
        Console.WriteLine(); 
        QC.resetColors(); 
        string crop = Console.ReadLine(); 
        Farm.plantCrop(crop); 
       } 
       if (input.ToLower() == menuContent.ElementAt(1)) 
       { 
        // miner 

       } 
       if (input.ToLower() == menuContent.ElementAt(2)) 
       { 
        // fight 

       } 
      } 





     } 
    } 
+1

你確定'growTimer.Start();'被調用嗎?在具有斷點或記錄的調試模式下。 –

+0

@ Pierre-LucPineault是的,它的值是growTimer {Interval = 2000},wheatTime = 2000 –

+0

你在哪裏叫'plantCrop(「wheat」)'? – phoog

回答

2

System.Windows.Forms.Timer定時器是爲具有單個UI線程的Windows窗體應用程序製作的。

您需要使用System.Threading.Timer計時器代替您的控制檯應用程序。

它的創建和回調的參數是有一點不同:

public static void InitGrowTimer(int time, string name) 
    { 
     growTimer = new System.Threading.Timer(GrowTimer_Finished, null, time, Timeout.Infinite); 
     currentPlant = name; 
    } 

    private static void GrowTimer_Finished(object sender) 
    { 
     MessageBox.Show("Your " + currentPlant + " is finished!", "Civilisation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 
    } 

你不需要任何Start方法,它會自動在創建時啓動。

你也不需要Stop;由於參數Timeout.Infinite,它將只運行一次。

如果需要(例如EventArgs),您可以用想要回叫接收的對象代替null


小方面說明:我已將PascalCase中的回調方法重命名。按照慣例,C#中的方法應始終以大寫字母開頭。

+0

的價值你確定他應該在調用WinForms'MessageBox.Show'時使用'System.Threading.Timer'嗎? – Enigmativity

+0

啊,這是有道理的,我明天會測試它的第一件事。我正在用駱駝套寫作,因爲這是我們在商店課程中使用的。 –

+0

@Enigmativity那是另一個問題,從另一個線程更新UI。那樣我推薦他閱讀[.NET 4中的並行性](http://reedcopsey.com/series/parallelism-in-net4/)。 – mijail

-2

因爲你,因爲你告訴自己,不要啓動定時器。

growTimer = new Timer(); 
growTimer.Tick += new EventHandler(growTimer_Finished); 
growTimer.Interval = time; 
growTimer.Start(); 
+0

它在'InitGrowTimer'方法調用下開始。 –

+0

@ Pierre-LucPineault可能是這個問題吧? –

+0

@Ian號將'Start'行移入方法不會改變任何內容。它在退出'InitGrowTimer'後立即被調用。除非你試圖從'plantCrop'之外的地方開始你的計時器。 –