博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
总结以下三种方法,实现c#每隔一段时间执行代码:
阅读量:4303 次
发布时间:2019-05-27

本文共 2081 字,大约阅读时间需要 6 分钟。

总结以下三种方法,实现c#每隔一段时间执行代码:

方法一:调用线程执行方法,在方法中实现死循环,每个循环Sleep设定时间;

方法二:使用System.Timers.Timer类;

方法三:使用System.Threading.Timer;

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using 
System;
using 
System.Collections;
using 
System.Threading;
 
public 
class 
Test
{
 
    
public 
static 
void 
Main()
    
{
        
Test obj =
new 
Test();
        
Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString());
 
        
//方法一:调用线程执行方法,在方法中实现死循环,每个循环Sleep设定时间
        
Thread thread =
new 
Thread(
new 
ThreadStart(obj.Method1));
        
thread.Start();
 
 
        
//方法二:使用System.Timers.Timer类
        
System.Timers.Timer t =
new 
System.Timers.Timer(100);
//实例化Timer类,设置时间间隔
        
t.Elapsed +=
new 
System.Timers.ElapsedEventHandler(obj.Method2);
//到达时间的时候执行事件
        
t.AutoReset =
true
;
//设置是执行一次(false)还是一直执行(true)
        
t.Enabled =
true
;
//是否执行System.Timers.Timer.Elapsed事件
        
while 
(
true
)
        
{
            
Console.WriteLine(
"test_" 
+ Thread.CurrentThread.ManagedThreadId.ToString());
            
Thread.Sleep(100);
        
}
 
 
        
//方法三:使用System.Threading.Timer
        
//Timer构造函数参数说明:
        
//Callback:一个 TimerCallback 委托,表示要执行的方法。
        
//State:一个包含回调方法要使用的信息的对象,或者为空引用(Visual Basic 中为 Nothing)。
        
//dueTime:调用 callback 之前延迟的时间量(以毫秒为单位)。指定 Timeout.Infinite 以防止计时器开始计时。指定零 (0) 以立即启动计时器。
        
//Period:调用 callback 的时间间隔(以毫秒为单位)。指定 Timeout.Infinite 可以禁用定期终止。
        
System.Threading.Timer threadTimer =
new 
System.Threading.Timer(
new 
System.Threading.TimerCallback(obj.Method3),
null
, 0, 100);
        
while 
(
true
)
        
{
            
Console.WriteLine(
"test_" 
+ Thread.CurrentThread.ManagedThreadId.ToString());
            
Thread.Sleep(100);
        
}
 
        
Console.ReadLine();
    
}
 
 
    
void 
Method1()
    
{
        
while 
(
true
)
        
{
            
Console.WriteLine(DateTime.Now.ToString() +
"_" 
+ Thread.CurrentThread.ManagedThreadId.ToString());
            
Thread.CurrentThread.Join(100);
//阻止设定时间
        
}
    
}
 
 
    
void 
Method2(
object 
source, System.Timers.ElapsedEventArgs e)
    
{
        
Console.WriteLine(DateTime.Now.ToString() +
"_" 
+ Thread.CurrentThread.ManagedThreadId.ToString());
    
}
 
 
    
void 
Method3(Object state)
    
{
        
Console.WriteLine(DateTime.Now.ToString() +
"_" 
+ Thread.CurrentThread.ManagedThreadId.ToString());
    
}
}

转载地址:http://enlws.baihongyu.com/

你可能感兴趣的文章
HTTPS
查看>>
git add . git add -u git add -A区别
查看>>
apache下虚拟域名配置
查看>>
session和cookie区别与联系
查看>>
PHP 实现笛卡尔积
查看>>
Laravel中的$loop
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel 操作redis的各种数据类型
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>