Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,

I thought I was beginning to understand the new async/await keywords. Then I had cause to write something like the code below, similar to an extremely slow game loop, which simply runs continuously until a flag changes value. This is a much-shortened version of the code:

C#
public static LoopFlag { get; set; } = true;
	
public static void MyEventHandler()
{
	Task.Factory.StartNew( () => WorkerLoop() );
}

private static async void WorkerLoop()
{
	do
	{
		Debug.WriteLine( "WorkerLoop()..." );
		await Task.Delay( 1000 );

	} while( LoopFlag );
}


Now, as I understand it, when the await keyword it encountered, control simply passes back to the caller, and a 'continuation marker' is placed in the code, a promise to come back to this point when the task has completed.

I also think I read that Task.Delay() creates a new Task that runs for the specified time before completing.

So, my question is, is the code above 'safe', or will it simply whiz round as fast as it can, creating thousands of new Task.Delay( 1000 ) objects?

Any advice would be gratefully received.

Kind wishes ~ Patrick

What I have tried:

I've tried something similar to the code above. It seems to do exactly what I want, but I'm not sure how to tell if I am ending up with thousands of objects piling up in the background.
Posted
Updated 19-Mar-16 2:54am
v2
Comments
Patrick Skelton 23-Mar-16 11:06am    
Still using something like the loop above, I have noticed that the the thread number keeps going up in the debugger. I get messages like:

Thread started: <Thread Pool> #8
Thread started: <Thread Pool> #9
Thread started: <Thread Pool> #10
Thread started: <Thread Pool> #11
Thread finished: <Thread Pool> #6
Thread finished: <Thread Pool> #8
Thread started: <Thread Pool> #12
Thread finished: <Thread Pool> #10

Note that I don't know if the total number of threads is rising, but the ID number does keep going up. Is this normal? (I can attempt to match start-finish messages, take all these away, and see how many threads are left, but I just wanted to ask here first, to see if I am worrying over nothing.)

1 solution

Task.Delay will create a new Task (technically, a DelayPromise) and a Timer:
Task.Delay | Reference Source[^]

However, in your example, it will do this once per second. It won't "whiz round" creating new objects in a tight loop, because the await ensures that the rest of the method doesn't execute until the task has completed. As soon as the task is completed, the Timer will be disposed, and the DelayPromise will be eligible for GC.
 
Share this answer
 
Comments
Patrick Skelton 19-Mar-16 8:53am    
I cannot believe I have lasted this long without knowing about http://referencesource.microsoft.com/.

Thank you - both for the answer above and learning about this new (to me) resource.

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