wpf - 使用函数开始调用?

标签 wpf vb.net 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

但是这个函数没有:

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/

相关文章:

json - 位置管理器和数据管理

c# - 将模型与 View 模型分开有什么意义? (MVVM)

c# - PreviewKeyDown 没有看到 Alt 修饰符

C# WPF - 任务与多线程,以及从中更新 UI 组件

.net - 使用 .Net Parallel Extensions (Parallel.Invoke) 进行多个异步调用?

JavaScript Promise 不等待异步调用

c# - 在不触发事件的情况下设置 WPF 依赖属性

.net - MySQL DataCONnection 和 ADO.NET

vb.net - WCF 服务生成的代理类将函数公开为 ByRef Sub

asp.net - 解决SQL注入(inject)问题