如何在执行大型任务的方法之间添加延迟



我有一个按钮单击事件,它将调用在远程服务器上启动服务的方法。每当我必须启动大量的服务器服务(如20(时,它就会失败。我如何才能确定所有服务都会正确启动而不会失败?我已经读到,您可以使用Thread或AsyncDelay在方法之间添加一些时间。我不确定哪一个最好用。任何建议都将不胜感激。提前感谢!

protected void StartServices_Click(object sender, EventArgs e)
{
ServerOne_Start();
// What should I add between these two methods that will provide some time
// for ServerOne_Start to execute before executing ServerTwo_Start?
ServerTwo_Start();
}

可能最简单的解决方案是将事件处理程序asyncawait设置为Task.Delay

protected async void StartServices_Click(object sender, EventArgs e)
{
ServerOne_Start();
await Task.Delay(TimeSpan.FromSeconds(5));
ServerTwo_Start();
}

最新更新