• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# Timer类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中System.Timers.Timer的典型用法代码示例。如果您正苦于以下问题:C# Timer类的具体用法?C# Timer怎么用?C# Timer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



Timer类属于System.Timers命名空间,在下文中一共展示了Timer类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Timers;

public class Example
{
   private static System.Timers.Timer aTimer;
   
   public static void Main()
   {
      SetTimer();

      Console.WriteLine("\nPress the Enter key to exit the application...\n");
      Console.WriteLine("The application started at {0:HH:mm:ss.fff}", DateTime.Now);
      Console.ReadLine();
      aTimer.Stop();
      aTimer.Dispose();
      
      Console.WriteLine("Terminating the application...");
   }

   private static void SetTimer()
   {
        // Create a timer with a two second interval.
        aTimer = new System.Timers.Timer(2000);
        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;
        aTimer.AutoReset = true;
        aTimer.Enabled = true;
    }

    private static void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}",
                          e.SignalTime);
    }
}
开发者ID:.NET开发者,项目名称:System.Timers,代码行数:37,代码来源:Timer

输出:

Press the Enter key to exit the application...

The application started at 09:40:29.068
The Elapsed event was raised at 09:40:31.084
The Elapsed event was raised at 09:40:33.100
The Elapsed event was raised at 09:40:35.100
The Elapsed event was raised at 09:40:37.116
The Elapsed event was raised at 09:40:39.116
The Elapsed event was raised at 09:40:41.117
The Elapsed event was raised at 09:40:43.132
The Elapsed event was raised at 09:40:45.133
The Elapsed event was raised at 09:40:47.148

Terminating the application...


示例2: Main

//引入命名空间
using System;
using System.Threading.Tasks;
using System.Timers;

class Example
{
   static void Main()
   {
      Timer timer = new Timer(1000);
      timer.Elapsed += async ( sender, e ) => await HandleTimer();
      timer.Start();
      Console.Write("Press any key to exit... ");
      Console.ReadKey();
   }

   private static Task HandleTimer()
   {
     Console.WriteLine("\nHandler not implemented..." );
     throw new NotImplementedException();
   }
}
开发者ID:.NET开发者,项目名称:System.Timers,代码行数:22,代码来源:Timer

输出:

Press any key to exit...
Handler not implemented...

Unhandled Exception: System.NotImplementedException: The method or operation is not implemented.
at Example.HandleTimer()
at Example.<
b__0>d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c__DisplayClass2.b__5(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch()


示例3: new Timer()

//引入命名空间
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
    public System.Timers.Timer timer1;
    private System.Windows.Forms.Label label1;
    public Form1() {
        this.timer1 = new System.Timers.Timer();
        this.label1 = new System.Windows.Forms.Label();
        ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
        this.SuspendLayout();
        // 
        // timer1
        // 
        this.timer1.Enabled = true;
        this.timer1.SynchronizingObject = this;
        this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimerElapsed);
        // 
        // label1
        // 
        this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        this.label1.ForeColor = System.Drawing.SystemColors.Highlight;
        this.label1.Location = new System.Drawing.Point(24, 8);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(224, 48);
        this.label1.TabIndex = 0;
        this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 69);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.label1});
        this.Text = "My Clock";
        this.Load += new System.EventHandler(this.Form1_Load);
        ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
        this.ResumeLayout(false);

    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }

    private void Form1_Load(object sender, System.EventArgs e) {
        timer1.Interval = 1000;
        timer1.Start();
        timer1.Enabled = true;
    }

    private void OnTimerElapsed(object sender, System.Timers.ElapsedEventArgs e) {
        label1.Text = DateTime.Now.ToString();
    }
}
开发者ID:C#程序员,项目名称:System.Timers,代码行数:60,代码来源:Timer


示例4: new Timer(new TimerCallback(TimerHandler), null, 5000, 10000)

//引入命名空间
using System;        
using System.Threading;

public class MainClass
{
  public static AutoResetEvent A = new AutoResetEvent(false);
  public static int index = 0;
  
  public static int Main(){
    Timer T = new Timer(new TimerCallback(TimerHandler), null, 5000, 10000);

    A.WaitOne();
    Console.WriteLine("Main Thread event signaled");
    T.Dispose();
    return 0;
  }
  public static void TimerHandler(object state)
  {
    Console.WriteLine("TimerHandler");
    if (index == 5)
      A.Set();

    index++;
    Console.WriteLine(index);
  }
}
开发者ID:C#程序员,项目名称:System.Timers,代码行数:27,代码来源:Timer



注:本文中的System.Timers.Timer类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# TimeZone类代码示例发布时间:2022-05-24
下一篇:
C# ElapsedEventArgs类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap