使用C#的计时器 [英] timer using C#

查看:79
本文介绍了使用C#的计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是做游戏的任务,然后我的问题是我必须为此做一个时间限制,但是我使用的代码无法正常工作...请有人帮我
tnx

i was task to make a game then my problem is that i have to make a time limitation for it but the code i used didn''t works...please somebody help me
tnx

推荐答案

确保正确设置间隔后已启用计时器.
Make sure you have enabled the timer after setting the interval correctly.


就像上面说的很难弄清楚你需要什么...

一个非常简单的计时器演示将如下所示:

Like stated above it''s hard to figure out what you need...

A very simple timer demo would look something like this:

class Program
    {
        static void Main(string[] args)
        {
            Demo demo = new Demo();
            demo.Run();
        }
    }

    class Demo
    {
        private Timer _timer;
        private int _iteration;
        private bool _gameOver;

        public void Run()
        {
            _gameOver = false;
            _iteration = 1;
            //init Timer (System.Timers.Timer)
            _timer = new Timer();
            _timer.Interval = 10000;
            _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
            _timer.Start();
            
            while (!_gameOver)
            {
                //game logic
                Console.WriteLine("Game Loop No." + _iteration.ToString());

                System.Threading.Thread.Sleep(500);
                _iteration++;
            }

            Console.WriteLine("Game over (timeout hit)");
            Console.ReadKey();
        }

        void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //timout hit
            _gameOver = true;
        }
    }


这篇关于使用C#的计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆