views:

411

answers:

2

Hey, how this is written in VB.NET? This was an example I found on http://www.codeproject.com/KB/silverlight/SynchronousSilverlight.aspx.

ThreadPool.QueueUserWorkItem(delegate
{
 var channelFactory = new ChannelFactory<ISimpleService>("*");
 var simpleService = channelFactory.CreateChannel();
 var asyncResult = simpleService.BeginGetGreeting("Daniel", null, null);
 string greeting = null;
 try
 {
  greeting = simpleService.EndGetGreeting(asyncResult);
 }
 catch (Exception ex)
 {
  DisplayMessage(string.Format(
    "Unable to communicate with server. {0} {1}", 
   ex.Message, ex.StackTrace));
 }
 DisplayGreeting(greeting);
});
+3  A: 

May be a few syntax errors but I am sure you can resolve them.

ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf GetGreeting))

Private Sub GetGreeting(o As Object)
    Dim channelFactory = New ChannelFactory(Of ISimpleService)("*")
    Dim simpleService = channelFactory.CreateChannel()
    Dim asyncResult = simpleService.BeginGetGreeting("Daniel", Nothing, Nothing)
    Dim greeting As String = Nothing
    Begin Try
        greeting = simpleService.EndGetGreeting(asyncResult)
    Catch ex As Exception
        DisplayMessage(String.Format("Unable to communicate with server. {0} {1}", ex.Message, ex.StackTrace))
    End Try
    DisplayGreeting(greeting)
End Sub
ChaosPandion
Thank you! I do not use this example on the Code Project directly:) It was mainly WaitCallback() I was looking for!
sv88erik
A: 

To provide a little explanation of the differences (other's have provided good code samples):

VB.NET doesn't support anonymous methods, which are supported in C# by using the delegate {} syntax to define an inline method. To convert that syntax to VB.NET you have to move the contents of the anonymous inline method out into a normal method, then use a Delegate pointed at the extracted method to initiate the call.

When both are compiled they are essentially the same, since anonymous methods in C# are really only anonymous in their pre-compiled state (the compiler generates names for the methods and then treats them as first-class methods).

STW