VB.NET - ThreadPool and Delegate to C # on VB.NET

Hey, how is it written in VB.NET? This was an example that I found at 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
source share
3 answers

There may be some syntax errors, but I'm 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
+3
source

In VB10 (VS2010) you can do a pretty literal translation:

    ThreadPool.QueueUserWorkItem(
          Sub()
           Console.WriteLine("Hello")
          End Sub)

And note that linecontinuations (_) are not needed here.

But you probably want this for VS2008, and then you need to split the delegate as a separate Sub or Function.

Sub Main()
    ThreadPool.QueueUserWorkItem(AddressOf CallBack, "Hello")
End Sub

Sub CallBack(ByVal state As Object)
   Console.WriteLine(state)
End Sub
+2
source

( ):

VB.NET , # delegate {} . VB.NET, , , , .

, , # ( , ).

0

Source: https://habr.com/ru/post/1727038/


All Articles