您的位置:首页 > 编程语言 > C#

c#实现每隔一段时间执行代码(多线程)

2012-02-17 22:12 369 查看
总结以下三种方法,实现c#每隔一段时间执行代码:

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

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

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

?
方法一:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TestThread
{
class Program
{
private static int Count=0;
static void Main(string[] args)
{
Thread mythread = new Thread(new ThreadStart(print));
mythread.Start();

Console.ReadLine();
}
private static void print()
{
while (true)
{
Count++;
Console.WriteLine(Count.ToString());
Console.WriteLine(DateTime.Now.ToString());
Thread.Sleep(1000);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: