Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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().

C#
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());
}
Posted
Updated 11-Nov-13 2:06am
v2
Comments
CPallini 11-Nov-13 8:16am    
Well, that means that program termination happens 'more immediately' than the timer execution. :-)
Brijesh Kr 11-Nov-13 8:25am    
suppose i write 10 or 20 Console.WriteLine("test") after new System.Threading.Timer(TimerDelegate, null, 0, 2000); and comment Sleep(1000). TimerTask() might be called since here i have given main thread change to be alive. Right?
CPallini 11-Nov-13 8:39am    
'Might'. That is, the I/O operations give a chance to the timer.
Brijesh Kr 11-Nov-13 8:53am    
Hmmmm, if you write Console.ReadLine();
Do me one favor please delete solution 2.
CPallini 11-Nov-13 13:00pm    
if you write Console.WriteLine too (please try yourself).

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.
 
Share this answer
 
Comments
Brijesh Kr 11-Nov-13 8:17am    
System.Threading.Timer(TimerDelegate, null, 0, 2000); Here the 3rd argument is duetime which is set to 0, the thread should start immediately. Right?
OriginalGriff 11-Nov-13 9:28am    
Well...yes...but...

What actually happens is that the thread goes into a queue of available-to-run threads that the system maintains - and if it gets the the head of the list and there is a core available in time then it will run. But...your main method doesn't do much, so it won't use up it's "slot" before it finishes adding the new thread to the systems list. So it terminates, and the new thread is also killed at the same time before it really gets a chance to run.

Just because a thread can run, doesn't mean it will - it has to "jostle" with all the other threads that are waiting for a slot to run in before it gets a go.
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());
        }
    }


it will show result

NOTE:your program will close as the last "Thread.Sleep" would be executed
 
Share this answer
 
Comments
Brijesh Kr 11-Nov-13 9:22am    
You are absolutely wrong my dear friend. the 9th line will .. NOT .. execute after 2 sec as you have written
Brijesh Kr 11-Nov-13 9:25am    
Better you should delete your description by your self.
agent_kruger 11-Nov-13 9:38am    
it will run after every 4 sec i tried

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900