这个子工作正常:
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
但是这个函数没有:
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
如何在上述函数中返回我的列表项?
谢谢!!!!!!
:) 魔力
最佳答案
Dispatcher.BeginInvoke() 是一种即发即弃的方法,委托(delegate)目标稍后在 UI 线程上运行。在你的情况下这还不够好,你需要等到目标运行才能获得返回值。请改用 Invoke() 方法:
Return DirectCast(Me.Dispatcher.Invoke(..), ListItem)
并使用 Func 而不是 Action。或 AddressOf,更“自然”的 VB.NET 方式。
关于wpf - 使用函数开始调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3996545/