I have a function that triggers a Task to start.
void function ()
{
new Task(async () =>
{
// await network operation
}).Start();
}
The function could get called many times very frequently. But I don't want the Task to be started each time, I want it to only start on the last trigger within let's say 5 seconds.
Here are the steps to what I want to do exactly
- function() gets called
- Have a delay for 5 seconds
- if function() was called within those 5 seconds, I wish to cancel any pending Tasks, and start a delay of 5 seconds all over again
- If function() was not called within those 5 seconds, I then wish to start the Task
Please guide me as how I would do that in C# .NET 4.5