首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何向C#控制台应用程序添加计时器

如何向C#控制台应用程序添加计时器
EN

Stack Overflow用户
提问于 2008-10-09 06:07:50
回答 10查看 298.8K关注 0票数 148

就是这样--如何向C#控制台应用程序添加计时器?如果你能提供一些示例代码,那就太好了。

EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2011-10-23 16:25:49

这很好,但是为了模拟一些时间流逝,我们需要运行一个需要一些时间的命令,这在第二个例子中非常清楚。

然而,使用for循环来执行某些功能的风格永远会占用大量设备资源,相反,我们可以使用垃圾收集器来执行类似的操作。

我们可以在同一本书中的代码中通过C#第三版看到这种修改。

代码语言:javascript
复制
using System;
using System.Threading;

public static class Program 
{
   private Timer _timer = null;
   public static void Main() 
   {
      // Create a Timer object that knows to call our TimerCallback
      // method once every 2000 milliseconds.
      _timer = new Timer(TimerCallback, null, 0, 2000);
      // Wait for the user to hit <Enter>
      Console.ReadLine();
   }

   private static void TimerCallback(Object o) 
   {
      // Display the date/time when this method got called.
      Console.WriteLine("In TimerCallback: " + DateTime.Now);
   }
}
票数 137
EN

Stack Overflow用户

发布于 2008-10-09 06:09:42

使用System.Threading.Timer类。

System.Windows.Forms.Timer主要设计为在单线程中使用,通常是Windows Forms UI线程。

在开发.NET框架的早期,还添加了一个System.Timers类。但是,通常建议使用System.Threading.Timer类,因为这只是System.Threading.Timer的一个包装器。

如果您正在开发VB.NET服务并需要定期运行计时器,还建议始终使用静态(在Windows中共享) System.Threading.Timer。这将避免可能过早地对timer对象进行垃圾回收。

下面是一个控制台应用程序中的计时器示例:

代码语言:javascript
复制
using System; 
using System.Threading; 
public static class Program 
{ 
    public static void Main() 
    { 
       Console.WriteLine("Main thread: starting a timer"); 
       Timer t = new Timer(ComputeBoundOp, 5, 0, 2000); 
       Console.WriteLine("Main thread: Doing other work here...");
       Thread.Sleep(10000); // Simulating other work (10 seconds)
       t.Dispose(); // Cancel the timer now
    }
    // This method's signature must match the TimerCallback delegate
    private static void ComputeBoundOp(Object state) 
    { 
       // This method is executed by a thread pool thread 
       Console.WriteLine("In ComputeBoundOp: state={0}", state); 
       Thread.Sleep(1000); // Simulates other work (1 second)
       // When this method returns, the thread goes back 
       // to the pool and waits for another task 
    }
}

出自杰夫·里希特的书“CLR Via C#”。顺便说一句,这本书描述了23章中3种计时器背后的基本原理,强烈推荐。

票数 74
EN

Stack Overflow用户

发布于 2014-06-16 02:06:18

让我们来找点乐子

代码语言:javascript
复制
using System;
using System.Timers;

namespace TimerExample
{
    class Program
    {
        static Timer timer = new Timer(1000);
        static int i = 10;

        static void Main(string[] args)
        {            
            timer.Elapsed+=timer_Elapsed;
            timer.Start(); Console.Read();
        }

        private static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            i--;

            Console.Clear();
            Console.WriteLine("=================================================");
            Console.WriteLine("                  DEFUSE THE BOMB");
            Console.WriteLine(""); 
            Console.WriteLine("                Time Remaining:  " + i.ToString());
            Console.WriteLine("");        
            Console.WriteLine("=================================================");

            if (i == 0) 
            {
                Console.Clear();
                Console.WriteLine("");
                Console.WriteLine("==============================================");
                Console.WriteLine("         B O O O O O M M M M M ! ! ! !");
                Console.WriteLine("");
                Console.WriteLine("               G A M E  O V E R");
                Console.WriteLine("==============================================");

                timer.Close();
                timer.Dispose();
            }

            GC.Collect();
        }
    }
}
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/186084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档