每隔一段时间自动执行一次某个方法

class Program
{
public static bool bStop = false;
static int x = 10;
static void Main(string[] args)
{
//定义线程
Thread LogThread = new Thread(new ThreadStart(DoService));
//设置线程为后台线程,那样进程里就不会有未关闭的程序了
LogThread.IsBackground = true;
if (bStop == false)
{
LogThread.Start();//起线程
}
Console.ReadKey();
}
private static void DoService()
{
while (true)
{
bStop = false;
SendToService();
System.Threading.Thread.Sleep(1000);//1000=1秒
}
}
private static void SendToService()
{
x++;
Console.WriteLine(x);
}
}

猜你喜欢

转载自www.cnblogs.com/zhujie-com/p/12190086.html