Reputation: 119
I have the following code in LINQPad:
async Task Main()
{
await PrintLoop(Print1());
}
async Task Print1()
{
Debug.WriteLine("Printing!");
}
//Print 2
//Print 3
//etc.
async Task PrintLoop(Task printer, int iterations = 3)
{
for (int i = 0; i < iterations; i++)
{
await printer;
}
}
I can't for the life of me figure out why I get the following output:
Printing!
As opposed to "Printing!" x3.
If I call Print1()
directly within the loop I get the following output:
Printing!
Printing!
Printing!
Printing!
Which kind of makes sense but isn't what I want to do. Instead, I would like for Print1
(or whichever method is passed as task
) to be invoked iterations
times.
Can someone help me understand what's up and down here? Thanks!
Upvotes: 3
Views: 4120
Reputation: 40948
You are passing the result of calling Print1()
to the method (a Task
). You aren't passing the method itself. So it's only called once at Print1()
. When you await printer;
, it's really just saying "yup, it happened" and moving on.
If you want to pass the method itself, so that it can be called inside PrintLoop
, then you need to accept a Func<Task>
(a method that returns a Task
).
Then you pass the method itself (Print1
) without calling it (not Print1()
)
async Task Main()
{
await PrintLoop(Print1); //not Print1()
}
async Task Print1()
{
Debug.WriteLine("Printing!");
}
async Task PrintLoop(Func<Task> printer, int iterations = 3)
{
for (int i = 0; i < iterations; i++)
{
await printer();
}
}
Upvotes: 5