等待造成主线程持有?

编程入门 行业动态 更新时间:2024-10-28 10:25:05
本文介绍了等待造成主线程持有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请参阅下面的code,这是我改编自以下网页:的www.$c$cguru/csharp/csharp/introduction-to-async-and-await-keywords-in-c-5.0.htm

Please see the code below, which I have adapted from the following webpage: www.codeguru/csharp/csharp/introduction-to-async-and-await-keywords-in-c-5.0.htm

Public Class Form1 Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load Dim program As New Program() Await program.PrintSumAsync() MsgBox("got here 1") End Sub End Class Class Program Public Async Function PrintSumAsync() As Task Dim value1 As Integer = Await GetValueAsync() Dim value2 As Integer = Await GetValueAsync() Console.WriteLine("Sum of two random numbers is: {0}", value1 + value2) End Function Private Async Function GetValueAsync() As Task(Of Integer) System.Threading.Thread.Sleep(5000) Dim random As Integer = ComputeValue() Return random End Function Private Function ComputeValue() As Integer MsgBox("got here 2") Return New Random().[Next](1, 1000) End Function End Class

我添加了一个调用的GetValueAsync进入睡眠状态,这样需要一段时间才能完成。

I have added a call to Sleep in the GetValueAsync so that it takes a while to finish.

我预计code达到msgbox1(来到这里1)msgbox2之前(来到这里2)。等待似乎停止主线程。我在想什么?我没有任何经验的新望等待关键字。我从.NET 3.5升级到4.5.2 .NET最近。

I expected the code to reach msgbox1 (got here 1) before msgbox2 (got here 2). Await seems to stop the main thread. What am I missing? I don't have any experience with the newish await keyword. I upgraded from 3.5 to .NET 4.5.2 recently.

更新根据戴维斯的答案我已经编辑了code如下:

Update Based on Davids answer I have edited the code as follows:

Public Class Form1 Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load test() MsgBox("got here 1") End Sub Public Async Sub test() Dim program As New Program() Await program.PrintSumAsync() 'Dim task As Task = program.PrintSumAsync() MsgBox("got here 2") End Sub End Class Class Program Public Async Function PrintSumAsync() As Task Dim value1 As Integer = Await GetValueAsync() Dim value2 As Integer = Await GetValueAsync() Console.WriteLine("Sum of two random numbers is: {0}", value1 + value2) End Function Private Async Function GetValueAsync() As Task(Of Integer) Try Await Task.Delay(5000) Dim random As Integer = ComputeValue() Return random Catch ex As Exception MsgBox(ex.ToString) End Try End Function Private Function ComputeValue() As Integer MsgBox("got here 2") Return New Random().[Next](1, 1000) End Function End Class

我怎样才能阻止主线程从之前的消息框完成2出现?

How can I stop the main thread from finishing before message box 2 appears?

推荐答案

等待未停止主线程,你是:

Await isn't stopping the main thread, you are:

System.Threading.Thread.Sleep(5000)

有什么的异步的发生在这个方法:

There's nothing asynchronous happening in this method:

Private Async Function GetValueAsync() As Task(Of Integer) System.Threading.Thread.Sleep(5000) Dim random As Integer = ComputeValue() Return random End Function

(我很惊讶,编译器不抱怨它,它不会用C#)。因此,即使方法签名和消费code装饰有异步和等待关键字,由此产生的code不是真正异步的。它的可以的是,但是,如果你等待该方法的异步操作。例如:

(I'm surprised the compiler isn't complaining about it. It does with C#.) So even though the method signature and consuming code is decorated with Async and Await keywords, the resulting code isn't actually asynchronous. It can be, however, if you await an asynchronous operation in that method. For example:

Private Async Function GetValueAsync() As Task(Of Integer) Await Task.Delay(5000) Dim random As Integer = ComputeValue() Return random End Function

这将使问题异步操作的整个堆栈,充分利用新的关键字。

This will make the entire stack of operations in question asynchronous, making full use of the new keywords.

请注意,然而,观察到的结果可能仍是相同的。看看顶层方式:

Note, however, that the observed results will probably still be the same. Take a look at the top level method:

Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load Dim program As New Program() Await program.PrintSumAsync() MsgBox("got here 1") End Sub

在该方法中的code仍然是不会执行第三行,直到之后的第二线已经期待已久,直到完成。该的最高层的(调用此处理程序UI)将继续等待的这个的方法,但这种方法会等到它的异步操作都完成后再继续。

The code in that method still isn't going to execute the third line until after the second line has been awaited until completion. The very top level (the UI invoking this handler) will continue without waiting on this method, but this method will wait until its asynchronous operations are complete before it continues.

为了得到你正在寻找的结果,你想捕捉工作,而不是等待它。事情是这样的:

In order to get the result you're looking for, you would want to capture the Task instead of awaiting it. Something like this:

Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load Dim program As New Program() Dim task As Task = program.PrintSumAsync() MsgBox("got here 1") ' do something with the task object. ' await it, provide it with a callback function for when it completes, etc. End Sub

的异步和等待关键词并不一定就一个新的线程函数发生。对于大多数的意图和目的,你可以经常把它们想象成简单的句法简化为一个 ContinueWith()在任务中封装方法的一部分对象。在任何给定的异步方法,需要有一个等待在其中放置 ContinueWith ()。如果没有等待中,然后在异步方法是不同步的。

The Async and Await keywords don't necessarily make a function happen on a new thread. For most intents and purposes, you can very often think of them as simply syntactic shorthand for wrapping part of a method in a ContinueWith() on a Task object. In any given Async method, there needs to be an Await at which to place that ContinueWith(). If there's no Await within it, then the Async method isn't asynchronous.

更多推荐

等待造成主线程持有?

本文发布于:2023-11-26 22:19:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635428.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主线

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!