在另一个窗口中的异步功能
我在运行ASYNC功能时遇到问题。我正在尝试进行异步调用,以设置一些Timmers来运行一些代码。我已经尝试使功能异步,我觉得自己做错了什么。我已经将功能范围缩小到两个功能,并在功能中添加了20秒的睡眠以测试问题,我想知道我的睡眠是否在暂停整个应用程序,还是我不正确地进行异步?正确方向的任何点都会有所帮助。先感谢您。
private async void RunAsyncExample(){
Task timersTask = mainWindow.StartTimers(); //This has a 20 sec sleep
mainWindow.PullReceipt(handoff); //This should not wait
await timersTask; //This should wait
}
以下是mainwindow.starttimers
public async Task<Task> StartTimers()
{
_log.LogError("starting timer");
System.Threading.Thread.Sleep(20000);
_log.LogError("ending timer");
return Task.CompletedTask;
}
I'm having issues with running a function as async. I'm trying to make an async call to set up some timmers to run some code. I've tried making the function async and I feel like I'm doing something wrong. I've narrowed down the functionality to two functions and added a 20-second sleep in the function to test out the issue and I'm wondering if my sleep is pausing the whole application or am I doing async incorrectly? Any point in the right direction would be helpful. Thank you in advance.
private async void RunAsyncExample(){
Task timersTask = mainWindow.StartTimers(); //This has a 20 sec sleep
mainWindow.PullReceipt(handoff); //This should not wait
await timersTask; //This should wait
}
Below is the mainWindow.StartTimers
public async Task<Task> StartTimers()
{
_log.LogError("starting timer");
System.Threading.Thread.Sleep(20000);
_log.LogError("ending timer");
return Task.CompletedTask;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下操作:
顺便说一句,您的代码有很多问题:
async
方法。等待
,您不应startTimers
没有启动任何计时器,因此它不是适当的名称。async void
仅适用于异步事件处理程序Try the following:
Incidentally, your code has many issues:
async
method if it doesn'tawait
Sleep
StartTimers
doesn't start any timer, so it is not an appropriate name.async void
is appropriate only for asynchronous event handlers