c# .net why does Task.Run seem to handle Func differently than other code?

前端 未结 6 1466
生来不讨喜
生来不讨喜 2021-01-01 16:07

The new Task.Run static method that\'s part of .NET 4.5 doesn\'t seem to behave as one might expect.

For example:

Task t = Task.Run(()=&         


        
6条回答
  •  悲&欢浪女
    2021-01-01 16:44

    Here's my stab at it:

    public class MyTest
    {
        public void RunTest()
        {
            Task t = Task.Run(new Func(MyIntReturningMethod));
            t.Wait();
            Console.WriteLine(t.Result);
        }
    
        public int MyIntReturningMethod()
        {
            return (5);
        }
    }
    

提交回复
热议问题