I am checking out this simple piece of code:
Expand|Select|Wrap|Line Numbers
- private void button1_Click(object sender, EventArgs e)
- {
- Stopwatch sw = Stopwatch.StartNew();
- Task Task1 = Task.Factory.StartNew(() =>
- {
- Console.WriteLine("Task1 started in : "+ " "+sw.ElapsedMilliseconds);
- Thread.Sleep(4000);
- Console.WriteLine("Task1 finished in : " + " " + sw.ElapsedMilliseconds);
- }
- );
- }
Task1 started in : 2 in milliseconds
Task1 finished in : 4015 in milliseconds.
The problem is, that if i count the time with an external device, just like the timer of my watch or my cellphone s,
the total time is 4,8 or 4,9 seconds, which i wouldn t expect that to happen.
The first thing i ve thought, is that it takes the ThreadPool about 800msec or 1 sec to create the thread so as
to execute this task, but even if it is so, shouldn t the stopwatch count the time precisely?
So , why is there a deviation between an external timer and the stopwatch ?
Thanx in advance.