是什么区别Task.WaitAll(),并Task.WhenAll()从异步CTP?您能提供一些示例代码来说明不同的用例吗?
我有一个方法,它可以完成两个独立的逻辑.我希望我可以运行他们都在同一时间 ..当这两个孩子的方法完成事后才继续.
我试图弄清楚async/await语法,但我只是不明白.
这是代码:
public PewPew SomeMethod(Foo foo)
{
var cats = GetAllTheCats(foo);
var food = GetAllTheFood(foo);
return new PewPew
{
Cats = cats,
Food = food
};
}
private IList<Cat> GetAllTheCats(Foo foo)
{
// Do stuff, like hit the Db, spin around, dance, jump, etc...
// It all takes some time.
return cats;
}
private IList<Food> GetAllTheFood(Foo foo)
{
// Do more stuff, like hit the Db, nom nom noms...
// It all takes …Run Code Online (Sandbox Code Playgroud) 考虑在没有等待的情况下使用async.
想想也许你误解了异步的作用.警告是完全正确的:如果您将方法标记为异步但不在任何地方使用等待,那么您的方法将不是异步的.如果调用它,方法中的所有代码将同步执行.
我想编写一个应该运行异步但不需要使用await的方法.例如,当使用线程时
public async Task PushCallAsync(CallNotificationInfo callNotificationInfo)
{
Logger.LogInfo("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId,
}
Run Code Online (Sandbox Code Playgroud)
我想要调用PushCallAsync并运行异步,不想使用等待.
我可以在C#中使用async而无需等待吗?