Moj*_*oDK 2 vb.net wpf asynchronous
这个子工作正常:
Private Sub UpdateInfo(ByVal text As String, ByVal timeStamp As DateTime)
If Me.lstStatus.Dispatcher.Thread Is System.Threading.Thread.CurrentThread Then
' Do stuff with
Else
Me.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, New Action(Of String, DateTime)(AddressOf UpdateInfo), text, timeStamp)
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
但是这个功能没有:
Private Function UpdateInfo(ByVal text As String, ByVal timeStamp As DateTime) As ListItem
If Me.lstStatus.Dispatcher.Thread Is System.Threading.Thread.CurrentThread Then
Dim l As New ListItem
' Do stuff with
Return l
Else
Me.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, New Action(Of String, DateTime)(AddressOf UpdateInfo), text, timeStamp)
' Above line doesn't return anything??
End If
End Function
Run Code Online (Sandbox Code Playgroud)
如何在上面的函数中返回我的listitem?
谢谢!!!!!
:) Mojo
Dispatcher.BeginInvoke()是一种即发即弃的方法,委托目标稍后在UI线程上运行.在你的情况下,这还不够好,你需要等到目标运行,这样你才能获得返回值.改为使用Invoke()方法:
Return DirectCast(Me.Dispatcher.Invoke(..), ListItem)
Run Code Online (Sandbox Code Playgroud)
并使用Func而不是Action.或者AddressOf,更"自然"的VB.NET方式.