Resolved WebService UI update with Thread [c#->to VB]

witschi

New member
Joined
Dec 16, 2009
Messages
3
Programming Experience
3-5
Hi all could some one help me translating this to vb.net :
VB.NET:
double value = 0;
double results = 0;

    ThreadPool.QueueUserWorkItem(delegate
    {
        ConvertTemperatureSoapClient client = new ConvertTemperatureSoapClient();
        results = client.ConvertTemp(
            value,
            TemperatureUnit.degreeFahrenheit,
            TemperatureUnit.degreeCelsius);

        this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate
        {
            this.tbResults.Text = Convert.ToString(results);
        }));
    });
}

Thank's!
 
Last edited:
Hi
Has you know VB does not support anonymous methods/lambda expressions with a statement body.

And I don't know how to translate this part :

VB.NET:
        this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate
        {
            this.tbResults.Text = Convert.ToString(results);
        }));
    });
This doesn't run in the good thread
VB.NET:
Me.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New UpdateUIDelegate(Function() tbResults.Text = client.ConvertTemp(value, TemperatureUnit.degreeFahrenheit, TemperatureUnit.degreeCelsius).ToString))

And if I make something like this and don't know how to passe value and client to my updateUI method

VB.NET:
Private Sub CallConvert(ByVal avalue As Object)
    Dim value = CDbl(avalue)
    Dim client As ConvertTemperatureSoapClient = New ConvertTemperatureSoapClient("ConvertTemperatureSoap")    
    Me.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New UpdateUIDelegate(AddressOf UpdateUI))
  End Sub


  Private Sub UpdateUI()
    tbResults.Text = client.ConvertTemp(value, TemperatureUnit.degreeFahrenheit, TemperatureUnit.degreeCelsius).ToString
  End Sub

Thank's for helping
 
Ok ok
So I found a solution, damn VB is sometimes more complicated than C#

PHP:
Class Window1
  Private Delegate Sub UpdateUIDelegate()

  Private Sub btnConvert_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)

    Dim value As Double = 0
    Dim results As Double = 0

    If (Double.TryParse(tbTemp.Text, value)) Then
      'Thread prevents application to freeze'
      ThreadPool.QueueUserWorkItem(AddressOf CallConvert, value)
    Else
      MessageBox.Show("Vous devez saisir une valeur de type 'double' dans la zone de saisie")
    End If
  End Sub

  Private Sub CallConvert(ByVal avalue As Object)
    Dim value = CDbl(avalue)
    Dim client As ConvertTemperatureSoapClient = New ConvertTemperatureSoapClient("ConvertTemperatureSoap")    
    'Prevent The calling thread cannot access this object because a different thread owns it.'
    Me.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New UpdateUIDelegate(Function() UpdateUI(avalue, client)))
  End Sub

  Private Function UpdateUI(ByVal value As Double, ByVal client As ConvertTemperatureSoapClient)
    tbResults.Text = client.ConvertTemp(value, TemperatureUnit.degreeFahrenheit, TemperatureUnit.degreeCelsius).ToString
    ' Need to return something cause VB lambda function (different in C#)'
    Return Nothing
  End Function
End Class
 

Latest posts

Back
Top