Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异步任务正在等待另一个任务_C#_Asynchronous_.net Core_Async Await - Fatal编程技术网

C# 异步任务正在等待另一个任务

C# 异步任务正在等待另一个任务,c#,asynchronous,.net-core,async-await,C#,Asynchronous,.net Core,Async Await,我有如下代码: private async Task<bool> DoAsyncThing() { await doOtherThings(); } private async Task<bool> DoAsyncThing2() { await doOtherThings2(); } private async Task<bool> SaveAll() { return await _context.SaveChangesAsync()

我有如下代码:

private async Task<bool> DoAsyncThing()
{
  await doOtherThings();
} 

private async Task<bool> DoAsyncThing2()
{
  await doOtherThings2();
} 

private async Task<bool> SaveAll()
{
   return await _context.SaveChangesAsync() > 0;
}

public async Task<bool> FirstBatchProcess()
{
    var tasks = new List<Task<bool>>();
    ...
    users.Foreach(user => {
        task.Add(this.DoAsyncThing());
    });
    await Task.WhenAll(tasks);
    return await this.SaveAll();
}

public async Task<bool> SecondBatchProcess()
{
    // get all data from batch 1 and then do calculation
    var tasks = new List<Task<bool>>();
    ...
    users.Foreach(user => {
        task.Add(this.DoAsyncThing2());
    });
    await Task.WhenAll(tasks);
    return await this.SaveAll();
}


public async Task<bool> ProcessAll()
{
    await this.FirstBatchProcess();
    await this.SecondBatchProcess();
}
专用异步任务DoAsyncThing()
{
等待其他事情的发生;
} 
专用异步任务DoAsyncThing2()
{
等待其他事情2();
} 
专用异步任务SaveAll()
{
return wait_context.SaveChangesAsync()>0;
}
公共异步任务FirstBatchProcess()
{
var tasks=新列表();
...
users.Foreach(user=>{
task.Add(this.DoAsyncThing());
});
等待任务。何时(任务);
return等待这个。SaveAll();
}
公共异步任务SecondBatchProcess()
{
//从批次1获取所有数据,然后进行计算
var tasks=新列表();
...
users.Foreach(user=>{
task.Add(this.DoAsyncThing2());
});
等待任务。何时(任务);
return等待这个。SaveAll();
}
公共异步任务ProcessAll()
{
等待此消息。FirstBatchProcess();
等待此消息。SecondBatchProcess();
}
在ProcessAll中,我希望在执行SecondBatchProcess之前先执行firstBatchProcess。因为我有一些来自FirstBatchPRocess的数据,稍后将在SecondBatchProcess中使用。如果我运行此代码,两者都将异步执行并导致错误,因为SecondBatchProcess需要从FirstBatchProcess生成的数据

注意:两个BatchProcess都包含多个异步循环,因此我使用Task.WhenAll()
如何等待第一个BatchProcess完成,然后执行第二个BatchProcess?

如何使用
Task.Factory.StartNew()

如果
doasynchthing()
使用UI执行某些操作,则应将
TaskScheduler.FromCurrentSynchronizationContext()
StartNew()一起使用

我希望有帮助


谢谢。更新

因此,当我调用Task.Wait()时,它将等待该任务完成 它将继续另一个进程

因为你编辑了你的问题,如果我理解正确(我在字里行间阅读)

答案是“是”
FirstBatchProcess
中启动的所有任务将在执行
SecondBatchProcess
之前完成

原创

创建一个任务,将在所有提供的任务都已完成时完成 已完成

我想您可能会被
wait
操作员弄糊涂了

wait运算符应用于异步方法中的任务,以 在执行方法时插入一个暂停点,直到 等待的任务完成。这项任务是正在进行的工作

它实际上在等待


所有任务都已完成。

所有任务都已完成
当所有任务完成时,您是否可以更简洁一点,因为您不清楚自己到底想要什么不应该发生什么,或者不应该发生什么?事实上,
this.SaveAll()
将在
等待任务.WhenAll(任务)之后调用
finishesWell,
this.SaveAll()
只有在
Task.whalls(tasks)
完成后才会发生。听起来你已经有你想要的了。你有理由认为
SaveAll()
发生得太快了吗?我有一种沉闷的感觉,这将成为一个关于async Wait的永无止境的教程。我本以为注释会更复杂,例如
waiting,waiting,waiting,等待…
etc@EmrahS在调用
Console.WriteLine(“waiting”)时,üngü记住一个线程可能是原始线程只有当等待被点击时,事情才可能开始,也可能不会开始happen@EmrahS当任务调度器从线程池中跳出线程时,是的。但是在某些情况下(特别是io情况),使用异步等待模式,情况并非如此much@EmrahSüngüyup,同样,如果您对一个死地址进行了一次异步ping,并且超时1秒,那么您可能会加载100个甚至更多,而不必担心线程池延迟。原因是它将等待完成端口,而不是占用非io线程。异步的哪一个可怕的可扩展优势await@EmrahSüngü试试这个例子(显然它在.NETFiddle上不起作用),因为他们不想做别人的事。把计数提高
Task.Factory.StartNew(() =>
{
     return DoAsyncThing();
}).ContinueWith(x =>
{
     if (x.Result)
        SaveAll();
});
await this.FirstBatchProcess();  // will wait for this to finish
await this.SecondBatchProcess(); // will wait for this to finish
private static async Task DoAsyncThing()
{
    Console.WriteLine("waiting");
    await Task.Delay(1000);
    Console.WriteLine("waited");
}

private static async Task SaveAll()
{
    Console.WriteLine("Saving");
    await Task.Delay(1000);
}

public static async Task ProcessAll()
{
    var tasks = new List<Task>();
    for (int i = 0; i < 10; i++)
    {
        tasks.Add(DoAsyncThing());
    }

    await Task.WhenAll(tasks);
    await SaveAll();
    Console.WriteLine("Saved");
}

public static void Main()
{
    ProcessAll().Wait();
    Console.WriteLine("sdf");
}
waiting
waiting
waiting
waiting
waiting
waiting
waiting
waiting
waiting
waiting
waited
waited
waited
waited
waited
waited
waited
waited
waited
waited
Saving
Saved
sdf