System.Threading.Timer类,它是如何工作的 [英] System.Threading.Timer class, How it works

查看:68
本文介绍了System.Threading.Timer类,它是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制台应用程序中有以下代码。

现在如果我运行它显示'11 / 11/2013'作为输出。

但是如果我发表评论行没有。 9(System.Threading.Thread.Sleep(1000);)相同的程序什么都没显示。根据我的知识,应该调用TimerTask()。

为什么它依赖于'System.Threading.Thread.Sleep(1000)'来调用TimerTask()。



I have following code in console application.
Now if i run it shows '11/11/2013' as output.
But if i make comment line no. 9 (System.Threading.Thread.Sleep(1000);) the same program shows nothing. As per my knowledge TimerTask() should have been called.
Why it depends on 'System.Threading.Thread.Sleep(1000)' for calling TimerTask().

class Program
    {
        static void Main(string[] args)
        {
 System.Threading.TimerCallback TimerDelegate =
                new System.Threading.TimerCallback(TimerTask);
System.Threading.Timer TimerItem =
                new System.Threading.Timer(TimerDelegate, null, 0, 2000);
System.Threading.Thread.Sleep(1000);
}
 private static void TimerTask(object StateObj)
        {
           Console.WriteLine(DateTime.Now.ToString());
}

推荐答案

为什么?

为什么你认为一个线程应该执行如果它所属的应用程序已被终止?



如果您取出 Sleep(1000),那么主方法立即结束,应用程序终止。此时,与应用程序关联的所有线程也将终止。因此,即使线程确实有轻微的运行机会(可能在不同的核心上),也没有时间在控制台输出关闭之前更新它。
Why?
Why do you think that a thread should be executed if the application it is part of has been terminated?

If you take out the Sleep(1000) then the Main method immediately ends and the application is terminated. At that point, all threads associated with the application are terminated as well. So even if the thread did get a slight chance of running (on a different core perhaps) there isn't time for the console output to be updated before it too is closed down.


第9行将在2秒后执行,因为你写了



System.Threading.Timer TimerItem = new System.Threading.Timer(TimerDelegate,null,0,2000) ;



,你的程序将在1秒后关闭,因为你打电话给



系统.Threading.Thread.Sleep(1000);



如果你多次拨打第9行这样的话



the 9th line will execute after 2 sec as you have written

"System.Threading.Timer TimerItem = new System.Threading.Timer(TimerDelegate, null, 0, 2000);"

and your program will get closed in just 1 second as you have called

"System.Threading.Thread.Sleep(1000);"

if you call 9th line multiple time like this

class Program
    {
        static void Main(string[] args)
        {
            System.Threading.TimerCallback TimerDelegate =
                new System.Threading.TimerCallback(TimerTask);
            System.Threading.Timer TimerItem = new System.Threading.Timer(TimerDelegate, null, 1, 4000);
            System.Threading.Thread.Sleep(1000);
            System.Threading.Thread.Sleep(1000);
            System.Threading.Thread.Sleep(1000);
            System.Threading.Thread.Sleep(1000);
            System.Threading.Thread.Sleep(1000);
            System.Threading.Thread.Sleep(1000);
            System.Threading.Thread.Sleep(1000);
            System.Threading.Thread.Sleep(1000);
            System.Threading.Thread.Sleep(1000);
            System.Threading.Thread.Sleep(1000);
            System.Threading.Thread.Sleep(1000);
            System.Threading.Thread.Sleep(1000);
        }

        private static void TimerTask(object StateObj)
        {
            Console.WriteLine(DateTime.Now.ToString());
        }
    }





它会显示结果



注意:您的程序将在最后执行Thread.Sleep时关闭



it will show result

NOTE:your program will close as the last "Thread.Sleep" would be executed


这篇关于System.Threading.Timer类,它是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆