我如何使用C#计时器 [英] how do i use the c# timer

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

问题描述

嗨!

我正在尝试使用C#(Windows窗体应用程序)制作骰子游戏.一直试图找出如何使用计时器功能,但无法解决.
如果15秒钟后仍未进行选择,那么应该做的是开始计算机与自己对战的游戏.如果有人可以帮助我,我将非常高兴!

Hi!

I''m trying to make a dice game in c# (a windows form application). Been trying to find out how to use the timer function but can''t work it out.
What it is supposed to do is to start a game where the computer is playing against itself if no choice has been done after 15sec. If anyone could help me with this i would be extremely happy!

推荐答案

Here[^] is a simple example using a timer.

As you will see from the comments in the code, a value of 1000 represents 1 second so in your case you would use 15000 (15 secs).

You then put the code you want to execute at that interval in the Tick() event handler.


谢谢您的提问.您可以按照下面的代码.

Thank you for your question. You can follow the bellow code.

public void SetTimer()
{
    System.Timers.Timer aTimer = new System.Timers.Timer();
    aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
    aTimer.Interval=10000;
    aTimer.Enabled=true;
}
// Specify what you want to happen when the Elapsed event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
            //your code
}




谢谢,
Mamun




Thanks,
Mamun


private void WaitForChoice()
{
    Timer1.Interval = 15000;  // milliseconds
    Timer1.Enabled = true;
}

private void Timer1_Tick(object sender, EventArgs e)
{
    Timer1.Enabled = false;
    StartGameDemo();
}


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

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