简体   繁体   English

如何使用 System.Threading.Timer 和 Thread.Sleep?

[英]How do I use System.Threading.Timer and Thread.Sleep?

I made a maze game.我做了一个迷宫游戏。 I need a ticking timer.我需要一个滴答作响的计时器。 I have tried to create a class like this:我试图像这样创建一个 class :

using System;
using System.Threading;

namespace Maze
{
    class Countdown
    {
        public void Start()
        {
            Thread.Sleep(3000);              
            Environment.Exit(1);
        }
    }
}

and calls the method Start() at the start of the code.并在代码开头调用方法 Start()。 After running it, I tried to move the avatar through the maze which failed.运行它后,我试图将化身穿过迷宫,但失败了。 If I'm not mistaken, the Thread.Sleep makes the rest of my code not to work anymore.如果我没记错的话,Thread.Sleep 会使我的代码的 rest 不再工作。 If there's a way I can do other things, please tell me.如果有办法我可以做其他事情,请告诉我。

The reason your current code isn't working is that calling Thread.Sleep() stops any execution on the current thread until the time given has elapsed.您当前的代码不起作用的原因是调用Thread.Sleep()会停止当前线程上的任何执行,直到给定的时间过去。 So if you call Countdown.Start() on your main game thread (which I guess you are doing), your game will freeze until the Sleep() call has finished.因此,如果您在主游戏线程上调用Countdown.Start() (我猜您正在这样做),您的游戏将冻结,直到Sleep()调用完成。


Instead, you'll want to use System.Timers.Timer相反,您需要使用System.Timers.Timer

Take a look at the MSDN documentation .查看MSDN 文档

UPDATE: Now hopefully matches more your scenario更新:现在希望更符合您的情况

public class Timer1
 {
     private int timeRemaining;

     public static void Main()
     {
         timeRemaining = 120; // Give the player 120 seconds

         System.Timers.Timer aTimer = new System.Timers.Timer();

         // Method which will be called once the timer has elapsed
         aTimer.Elapsed + =new ElapsedEventHandler(OnTimedEvent);

         // Set the Interval to 3 seconds.
         aTimer.Interval = 3000;

         // Tell the timer to auto-repeat each 3 seconds
         aTimer.AutoReset = true;

         // Start the timer counting down
         aTimer.Enabled = true;

         // This will get called immediately (before the timer has counted down)
         Game.StartPlaying();
     }

     // Specify what you want to happen when the Elapsed event is raised.
     private static void OnTimedEvent(object source, ElapsedEventArgs e)
     {
         // Timer has finished!
         timeRemaining -= 3; // Take 3 seconds off the time remaining

         // Tell the player how much time they've got left
         UpdateGameWithTimeLeft(timeRemaining);
     }
 }

You're looking for the Timer class.您正在寻找Timer class。

Why not use one of the Timer classes already included in the BCL?为什么不使用 BCL 中已经包含的 Timer 类之一?

Here is a comparison of the different ones (MSDN Magazine - Comparing the Timer Classes in the .NET Framework Class Library).是不同的比较(MSDN 杂志 - 比较 .NET 框架 Class 库中的计时器类)。 Read it to see which one will be most suitable to your specific situation.阅读它,看看哪一个最适合您的具体情况。

In addition to @Slaks resposnse can say you can use:除了@Slaks resposnse 可以说你可以使用:

  1. System.Windows.Forms.Timer which is Timer on the same thread where UI stays System.Windows.Forms.Timer这是 UI 所在线程上的 Timer
  2. System.Timers.Timer which is a timer but runs on another thread. System.Timers.Timer这是一个计时器,但在另一个线程上运行。

Choose is up to you, depends on your app architecture.选择取决于您,取决于您的应用架构。

Regards.问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM