执行耗时的方法时WPF标签未更新
我几乎不好意思问这个问题,但是。。。 编辑: 我编辑了这个问题以显示我想出的代码。它不起作用。。。主窗体上看不到“测试”标题执行耗时的方法时WPF标签未更新,wpf,vb.net,multithreading,Wpf,Vb.net,Multithreading,我几乎不好意思问这个问题,但是。。。 编辑: 我编辑了这个问题以显示我想出的代码。它不起作用。。。主窗体上看不到“测试”标题 Private _dispatcher As Dispatcher Delegate Sub SetLabelText(caption As String) Private _setLabelText As SetLabelText Public Sub New() _setLabelText = New SetLabelText(AddressOf SetCo
Private _dispatcher As Dispatcher
Delegate Sub SetLabelText(caption As String)
Private _setLabelText As SetLabelText
Public Sub New()
_setLabelText = New SetLabelText(AddressOf SetConnectionStatus)
_dispatcher = Dispatcher.CurrentDispatcher
',,,
End Sub
Public Sub Connect
Try
_dispatcher.BeginInvoke(DispatcherPriority.Normal, _setLabelText, "test")
ConnectionStatus = "Connecting to server..."
_client = WCFClient.GetClient
Response = _client.GetInfo(Request)
ConnectionStatus = "Connected to server"
Catch ex As System.TimeoutException
ConnectionStatus="Timeout"
Catch ex As System.ServiceModel.EndpointNotFoundException
ConnectionStatus="Server down"
End Try
End Sub
Private Sub SetConnectionStatus(statusMessage As String)
ConnectionStatus = statusMessage
End Sub
Private _connectionStatus As String
Public Property ConnectionStatus() As String
Get
Return _connectionStatus
End Get
Set(ByVal value As String)
_connectionStatus = value
OnPropertyChanged("ConnectionStatus")
End Set
End Property
Private Sub OnPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
这段代码存在于我的ViewModel中,对于WPF表单,更改通知是通过INotifyPropertyChanged实现的。ConnectionStatus属性绑定到表单上标签的内容
从未显示“连接到服务器…”消息,仅显示三条结果状态消息中的一条。显然,我必须将一些任务转移到另一个线程。最简单的方法是什么?使用Dispatcher或BackgroundWorker 调度员: 背景工作人员:
使用Dispatcher或BackgroundWorker 调度员: 背景工作人员:
在提出问题之前,我已经访问了这两个url。我似乎无法在VB中完成它。好吧,很抱歉这个固定的答案。以下是使用BackgroundWorker更新ProgressBar的示例:。关键是知道你何时在子线程和UI线程中。在问这个问题之前,我已经在这两个url中找到了。我似乎无法在VB中完成它。好吧,很抱歉这个固定的答案。以下是使用BackgroundWorker更新ProgressBar的示例:。关键是知道你何时在子线程和UI线程中。不要感到羞愧。这是一个公平的问题,如果你不太舒服的线程。不要感到羞愧。这是一个公平的问题,如果你不太舒服的线程。