System.Threading.Timer多线程计时器

System.Threading.Timer t2 = null;

t2 = new System.Threading.Timer(new System.Threading.TimerCallback(timerCall), this, 30000, 60000);//预留30s进行检测

https://jingyan.baidu.com/article/8ebacdf024593049f65cd5cd.html

【1】第一个timer对象的变量, System.Threading.Timer ts;  

给ts赋值

ts =  new System.Threading.Timer(new TimerCallback(A), B, C, D);

A表示要执行的方法,可以带参数也可以不带参数

B表示要给这A方法传递的参数,如果A方法不带参数,B可以为空

C表示这个方法调用之前等待的时间

D表示这个方法多久调用一次

【2】示例代码

示例代码如图所示,我贴出完整代码,给大家参考

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            Student s = new Student();

            Console.Read();

           

        }

    }

 

    public class Student

    {

 

        public Student()

        {

            //定义一个对象

            System.Threading.Timer timer = new System.Threading.Timer(

                new System.Threading.TimerCallback(Say), null,

                10, 600);//这里是以秒计时的

        }

 

        //这里的参数必须是object开头的

        public void Say(object a )

        {

            Console.WriteLine("你好");

        }

 

    }

}

【3】该线程的方法的参数是必须是object

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值