views:

552

answers:

1

I am trying to use the code from Microsoft for an Async Socket connection. It appears the listener runs in the main thread locking the GUI. I am new at both socket connections and multi-threading all at the same time. Having a hard time getting my mind wrapped around this all at once.

The code used is at http://msdn.microsoft.com/en-us/library/fx6588te.aspx Using this example, how can I move the listener to its own thread?

 Public Shared Sub Main()
    ' Data buffer for incoming data.
    Dim bytes() As Byte = New [Byte](1023) {}

    ' Establish the local endpoint for the socket.
    Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim ipAddress As IPAddress = ipHostInfo.AddressList(1)
    Dim localEndPoint As New IPEndPoint(ipAddress, 11000)

    ' Create a TCP/IP socket.
    Dim listener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

    ' Bind the socket to the local endpoint and listen for incoming connections.
    listener.Bind(localEndPoint)
    listener.Listen(100)
+1  A: 

You can simply invoke the Main method of the socket in an asynchronous manner. You can use either:

Call New Action(AddressOf _
                AsynchronousSocketListener.Main).BeginInvoke(Nothing, Nothing)

or:

Call New Threading.Thread(AddressOf AsynchronousSocketListener.Main).Start()

(or use a BackgroundWorker)

M.A. Hanin
That looks like that helps! I think that even helps me understand it a bit.
TheHockeyGeek