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

前端 未结 6 1480
生来不讨喜
生来不讨喜 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:25

    The approach of Tyler Jensen works for me.

    Also, you can try this using a lambda expression:

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

提交回复
热议问题