WPF标签在执行耗时的方法时不会更新

时间:2011-12-26 18:28:58

标签: 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 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属性绑定到表单上的Label的内容。

永远不会显示“正在连接服务器..”消息,只显示三个结果状态消息中的一个。显然我必须将一些任务移到另一个线程。最简单的方法是什么?

1 个答案:

答案 0 :(得分:0)