C# Timer用法及实例详解

关于C# Timer类  在C#里关于定时器类就有3个

C# Timer使用的方法1.定义在System.Windows.Forms里

C# Timer使用的方法2.定义在System.Threading.Timer类里  "

C# Timer使用的方法3.定义在System.Timers.Timer类里

◆System.Windows.Forms.Timer

应用于WinForm中的,它是通过Windows消息机制实现的,类似于VB或Delphi中的Timer控件,内部使用API  SetTimer实现的。它的主要缺点是计时不精确,而且必须有消息循环,Console  Application(控制台应用程序)无法使用。   
  
◆System.Timers.Timer

和System.Threading.Timer非常类似,它们是通过.NET  Thread  Pool实现的,轻量,计时精确,对应用程序、消息没有特别的要求。

◆System.Timers.Timer还可以应用于WinForm,完全取代上面的Timer控件。它们的缺点是不支持直接的拖放,需要手工编码。

C# Timer用法实例

使用System.Timers.Timer类

1
2
3
4
5
6
7
8
9
System.Timers.Timer t = new System.Timers.Timer(10000);   //实例化Timer类,设置间隔时间为10000毫秒;   
    t.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到达时间的时候执行事件;   
    t.AutoReset = true;   //设置是执行一次(false)还是一直执行(true);   
    t.Enabled = true;     //是否执行System.Timers.Timer.Elapsed事件;   
     
    public void theout(object source, System.Timers.ElapsedEventArgs e)   
     {   
        MessageBox.Show("OK!");   
     }
 
 本回答由电脑网络分类达人 汪雷 推荐
评论

0 0

xiangjuan314

采纳率:95% 来自团队:电脑网络行家 擅长: 佛山市 烦恼 电脑/网络 生活 文化/艺术

其他回答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int a = 0;
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }
         
        private void timer1_Tick(object sender, EventArgs e)
        {
            a += 1;
            progressBar1.Value = a;
             
            if (a == 5) {
                Form2 frm = new Form2();
                timer1.Enabled = false;
                a = 0;
                frm.Show();
                this.Hide ();
             }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            progressBar1.Maximum = 5;
            progressBar1.Minimum =0;
            progressBar1.Value = 0;
            timer1.Interval = 1000;
        }
    }
}

C# 如何用计时器Timer控件实现停留几秒再做切换窗体的操作的更多相关文章

  1. winfrom弹出窗口用timer控件控制倒计时20秒后关闭

    功能描述: 因为在程序退出时需要确认是否是误操作,所以加了密码输入的子窗体,子窗体在20秒内会自动关闭 代码如下: private int count; private void Form2_Load ...

  2. timer控件、三级联动

    timer控件: 实现时间日期自增长: using System; using System.Collections.Generic; using System.ComponentModel; usi ...

  3. C# windows服务:C#windows服务中的Timer控件的使用

    C# windows服务程序中的Timer控件的使用问题是如何解决的呢? 今天和同事一起研究了下C# windows服务程序中的Timer控件的使用的写法. 我们在建立一个C# windows服务程序 ...

  4. Timer控件

    Timer控件是定期引发事件的控件,时间间隔的长度由interval属性定义,其值以毫秒为单位吗,若启用了该组件,则每个事件间隔引发一个Tick事件,Timer组件的主要方法包括start和stop, ...

  5. C# Winfrom 简单的运用Timer控件

    注意,在使用DateAndTime时,需要添加引用 using Microsoft.VisualBasic;否则不可以计算时间之间的差值. using System; using System.Col ...

  6. C# WF 第12节 Timer控件

    本节内容: 1:Timer控件的简介 2:实例1  : 不停的弹出,恶意exe 3:实例2: :流水灯 4:实例3:给流水灯加上计时器和在规定的时间进行播放音乐 1:Timer控件的简介 2:实例1 ...

  7. winform 用户控件、 动态创建添加控件、timer控件、控件联动

    用户控件: 相当于自定义的一个panel 里面可以放各种其他控件,并可以在后台一下调用整个此自定义控件. 使用方法:在项目上右键.添加.用户控件,之后用户控件的编辑与普通容器控件类似.如果要在后台往窗 ...

  8. WinForm用户控件、动态创建添加控件、timer控件--2016年12月12日

    好文要顶 关注我 收藏该文 徐淳 关注 - 1 粉丝 - 3       0 0     用户控件: 通过布局将多个控件整合为一个控件,根据自己的需要进行修改,可对用户控件内的所有控件及控件属性进行修 ...

  9. winform用户控件、动态创建添加控件、timer控件、控件联动

    用户控件: 相当于自定义的一个panel 里面可以放各种其他控件,并可以在后台一下调用整个此自定义控件. 使用方法:在项目上右键.添加.用户控件,之后用户控件的编辑与普通容器控件类似.如果要在后台往窗 ...

随机推荐

  1. JAX-WS:背后的技术JAXB及传递Map

    转载:http://www.programgo.com/article/98912703200/ 1.什么是JAX-WS JAX-WS (JavaTM API for XML-Based Web Se ...

  2. PHPStorm怎么修改选中的背景颜色呢?

    File -> Settings -> editor ->color&fonts->general->下拉框中的selection background-> ...

  3. ORA-32004 参数设置过时的解决办法

    启动时报错: 查看日志: view /opt/oracle11g/app/db/diag/rdbms/yldev/yldev/trace/alert_yldev.log 原来是plsql_debug ...

  4. UIImageView 的contentMode属性

    UIViewContentModeScaleToFill UIViewContentModeScaleAspectFit UIViewContentModeScaleAspectFill UIView ...

  5. 转:如何学习SQL(第四部分:DBMS扩展功能与SQL高级话题)

    转自:http://blog.163.com/mig3719@126/blog/static/285720652010950102575/ 9. DBMS提供的扩展功能 掌握了基本的关系模型原理和DB ...

  6. Android控件之ToggleButton(多状态按钮)

    一.概述 ToggleButton有两种状态:选中状态和没选中状态(类似一个开关),并且需要为不同的状态设置不同的显示文本 二.ToggleButton属性 android:checked = &qu ...

  7. 使用Python获取Linux系统的各种信息

    哪个Python版本? 当我提及Python,所指的就是CPython 2(准确的是2.7).我会显式提醒那些相同的代码在CPython 3 (3.3)上是不工作的,以及提供一份解释不同之处的备选代码 ...

  8. 转 java 类 单例

    转 单例概念: java中单例模式是一种常见的设计模式,单例模式分三种:懒汉式单例.饿汉式单例.登记式单例三种. 单例模式有一下特点: 1.单例类只能有一个实例. 2.单例类必须自己自己创建自己的唯一 ...

  9. eclipse 新建 maven 项目 步骤(初级入门新手)

    安装 maven(百度) 和在eclipse 中svn(上一篇) 修改 maven 本地仓库 eclipse 属性 maven--> installations-->添加新的 自定义安装的 ...

  10. 在线读取Mongodb数据库下载EXCEL文件

    版本:Mongodb2.4.8 通过页面下载Excel文件 jsp <%@ page language="java" contentType="text/html; ...