Zaman Uyumsuz (Visual Basic)
Değiştirici, Async yöntemin veya lambda ifadesinin zaman uyumsuz olduğunu gösterir. Bu tür yöntemler zaman uyumsuz yöntemler olarak adlandırılır.
Zaman uyumsuz yöntem, çağıranın iş parçacığını engellemeden potansiyel olarak uzun süreli çalışma yapmak için kullanışlı bir yol sağlar. Zaman uyumsuz bir yöntemin çağıranı, zaman uyumsuz yöntemin bitip bitmeden işini sürdürebilirsiniz.
Not
ve Async anahtar Await sözcükleri 2012 Visual Studio tanıtıldı. Zaman uyumsuz programlamaya giriş için bkz. Async ve Awaitile Zaman Uyumsuz Programlama.
Aşağıdaki örnek, bir zaman uyumsuz metodun yapısını gösterir. Kural olarak, zaman uyumsuz metot adları "Async." ile biter.
Public Async Function ExampleMethodAsync() As Task(Of Integer)
' . . .
' At the Await expression, execution in this method is suspended and,
' if AwaitedProcessAsync has not already finished, control returns
' to the caller of ExampleMethodAsync. When the awaited task is
' completed, this method resumes execution.
Dim exampleInt As Integer = Await AwaitedProcessAsync()
' . . .
' The return statement completes the task. Any method that is
' awaiting ExampleMethodAsync can now get the integer result.
Return exampleInt
End Function
Genellikle anahtar sözcüğü tarafından değiştirilen bir Async yöntem en az bir Await ifadesi veya deyimi içerir. Bekleyen görev tamamlanıncaya kadar bekletilen nokta olan ilk Await'a ulaşana kadar metot zaman uyumlu olarak çalışır. Bu sırada, denetim, metodu çağırana döner. yöntemde bir ifade veya deyim yoksa, yöntem askıya alınmamıştır ve zaman uyumlu Await bir yöntem gibi yürütülür. Derleyici uyarısı, bu durum bir hata belirteci olabileceği için içermeen zaman Await uyumsuz yöntemlerle ilgili sizi uyarıyor. Daha fazla bilgi için derleyici hatasına bakın.
Async anahtar kelimesi ayrılmamış bir anahtar sözcüktür. Bir metot veya lambda ifadesi değiştirdiği zaman bir anahtar sözcüktür. Diğer tüm bağlamlarda bu, bir tanımlayıcı olarak yorumlanır.
Dönüş Türleri
Zaman uyumsuz yöntem, bir Alt yordam veya veya dönüş türüne sahip bir İşlev Task Task<TResult> yordamıdır. yöntemi herhangi bir ByRef parametresi bildirilemez.
Yöntemin Return deyimi TResult türünde bir işlenene sahipse, zaman uyumsuz Task(Of TResult) yöntemin dönüş türü için belirtirsiniz. Yöntem Task tamamlandığında anlamlı bir değer döndürülene kadar kullanırsanız kullanın. Yani, yönteme bir çağrı, Task'i geri getirir, ancak Task tamamlandığı zaman, Await'i bekleyen herhangi bir Task deyimi bir sonuç değeri üretemez.
Zaman uyumsuz alt rutinler, öncelikle bir Sub yordamının gerekli olduğu yerde olay işleyicilerini tanımlamak için kullanılır Zaman uyumsuz bir alt rutinin çağırıcısı onu bekleyemez ve metodun oluşturduğu özel durumları yakalayamaz.
Daha fazla bilgi ve örnekler için bkz. Zaman Uyumsuz Dönüş Türleri.
Örnek
Aşağıdaki örnekler, zaman uyumsuz bir olay işleyicisi, bir zaman uyumsuz lambda ifadesi ve bir zaman uyumsuz metot gösterir. Bu öğeleri kullanan tam bir örnek için bkz. Walkthrough: Accessing the Web by Using Async and Await. Örneği .NET Sample Browser'dan indirebilirsiniz. Örnek kod SerialAsyncExample projesindedir.
' An event handler must be a Sub procedure.
Async Sub button1_Click(sender As Object, e As RoutedEventArgs) Handles button1.Click
textBox1.Clear()
' SumPageSizesAsync is a method that returns a Task.
Await SumPageSizesAsync()
textBox1.Text = vbCrLf & "Control returned to button1_Click."
End Sub
' The following async lambda expression creates an equivalent anonymous
' event handler.
AddHandler button1.Click, Async Sub(sender, e)
textBox1.Clear()
' SumPageSizesAsync is a method that returns a Task.
Await SumPageSizesAsync()
textBox1.Text = vbCrLf & "Control returned to button1_Click."
End Sub
' The following async method returns a Task(Of T).
' A typical call awaits the Byte array result:
' Dim result As Byte() = Await GetURLContents("https://msdn.com")
Private Async Function GetURLContentsAsync(url As String) As Task(Of Byte())
' The downloaded resource ends up in the variable named content.
Dim content = New MemoryStream()
' Initialize an HttpWebRequest for the current URL.
Dim webReq = CType(WebRequest.Create(url), HttpWebRequest)
' Send the request to the Internet resource and wait for
' the response.
Using response As WebResponse = Await webReq.GetResponseAsync()
' Get the data stream that is associated with the specified URL.
Using responseStream As Stream = response.GetResponseStream()
' Read the bytes in responseStream and copy them to content.
' CopyToAsync returns a Task, not a Task<T>.
Await responseStream.CopyToAsync(content)
End Using
End Using
' Return the result as a byte array.
Return content.ToArray()
End Function