2

I am nearly ashamed to ask this, but... EDIT: I edited the question to show the code I have come up with. It does not work... No "test" caption becomes visible on the main form.

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

This bit of code lives in my ViewModel for a WPF form, change notification is implemented through INotifyPropertyChanged. The ConnectionStatus property is bound to the Content of a Label on the form.

The 'Connecting to server..' message is never shown, only one of the three resulting statusmessages. Obviously I have to move some task to an other thread. What is the easiest way to do it?

Dabblernl
  • 15,831
  • 18
  • 96
  • 148

1 Answers1

0

Either use Dispatcher or a BackgroundWorker.

Dispatcher: http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx

BackgroundWorker: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

Xcalibur37
  • 2,305
  • 1
  • 17
  • 20
  • I have been at both url's before asking the question. I just cannot seem to be able to get it done in VB. – Dabblernl Dec 26 '11 at 20:05
  • Okay, sorry for the canned answer. Here is an example of updating a ProgressBar with a BackgroundWorker: http://stackoverflow.com/questions/8465662/update-progress-bar-in-mainwindow-from-a-different-thread/8465764#8465764. The key is knowing when you are in the child thread and the UI thread. – Xcalibur37 Dec 26 '11 at 20:19