Following up with solutions provided in many questions around Fire and Forget. My scenario is that I want to run the Forgotten events in order they were triggered.
My solution is based on Simplest way to do a fire and forget method in c# 4.0
Here is my test code. And as expected it never guaranteed what order they will be processed.
static void Main(string[] args)
{
Test(5);
Console.WriteLine("5 sent");
Test(2);
Console.WriteLine("2 sent");
Test(1);
Console.WriteLine("1 sent");
Test(4);
Console.WriteLine("4 sent");
Console.WriteLine("all sent");
Console.ReadLine();
}
private static void Test(int messageNumber)
{
Action myMethod = () => {
Task.Delay(messageNumber * 1000);
Console.WriteLine(messageNumber);
};
Blindly.Run(myMethod);
}
and my output
5 sent
2 sent
1 sent
4 sent
all sent
2
1
5
4
Imagine, you are using Fire and Forget for logging. So you don't want to block the calling code, but want to write all logged data in order they occurred.