I have a series of methods (with variable number of parameters) that return a Task I want to create a method that does something before and after each of this methods, by passing the Task. I've simplified everything (removed cancellationToken, actual processing, etc) in this sample:
public async Task<string> GetDataString()
{
Console.WriteLine("Executing");
return "test";
}
public async Task<T> Process<T>(Task<T> task)
{
Console.WriteLine("Before");
var res = await task;
Console.WriteLine("After");
return res;
}
And in my main:
Task<string> task = GetDataString();
string result = await Process<string>(tasks);
Console.WriteLine(res);
the console output is
Executing
Before
After
test
What can I do to create the task but not actually starting it? And starting it only before the wait?
I managed to do it by creating a PauseToken, as explained in this article: https://devblogs.microsoft.com/pfxteam/cooperatively-pausing-async-methods/ but I wonder if is there a better way.
Thanks, Mattia