我有以下的代码,并在.NET 4.5C#Task.Run相当于在.NET 4.0中
return Task.Run(async() => await MyAsyncMethodThatReturnsAnObject()).Result;
工作正常,现在我需要它.NET 4.0运行,所以我想出了:
return Task.Factory.StartNew<object>(async() => { await MyAsyncMethodThatReturnsAnObject()}).Result;
但它给我这个错误:
'Task<object>' does not contain a definition for GetAwaiter and no extension method 'GetAwaiter' acception the first argument of type 'Task<object>'
MyAsyncMethodThatReturnsAnObject
是一种工厂方法,其返回类型为object
。
委托'异步()=>等待MyAsyncMethodThatReturnsAnObject()'等同于'工作()=> MyAsyncMethodThatReturnsAnObject()'两个案例。 – spender
更好的问题是你为什么这样做?你释放一个线程来阻止另一个? –
@spender你的建议使我的代码编译,这是伟大的。但是,当我在调用方中做出相同的更改时,它失败了:'不能转换表达式类型'System.Threading.Tasks.Task'返回类型'System.Threading.Tasks.Task