trying to capture the TPL.
Just for fun, I tried to create some Random Sleep Quests to see how it was handled. I aimed at the fire and forgot the drawing.
static void Main(string[] args) { Console.WriteLine("Demonstrating a successful transaction"); Random d = new Random(); for (int i = 0; i < 10; i++) { var sleep = d.Next(100, 2000); Action<int> succes = (int x) => { Thread.Sleep(x); Console.WriteLine("sleep={2}, Task={0}, Thread={1}: Begin successful transaction", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, x); }; Task t1 = Task.Factory.StartNew(() => succes(sleep)); } Console.ReadLine(); }
But I don’t understand why it prints all lines to the console, ignoring Sleep (random)
Can someone explain this to me?
source share