2010-03-26 44 views
0

我正在嘗試使用Microsoft的代碼作爲異步套接字連接。看起來監聽器在鎖定GUI的主線程中運行。我同時在套接字連接和多線程中都是新手。我很難一下子把這件事全部包裹起來。單獨線程上的異步套接字偵聽器 - VB.net

使用的代碼是http://msdn.microsoft.com/en-us/library/fx6588te.aspx 使用此示例,如何將偵聽器移動到其自己的線程?

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

您可以簡單地以異步方式調用套接字的Main方法。您可以使用:

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

或:

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

(或使用BackgroundWorker

+0

這看起來像幫助!我認爲這甚至可以幫助我理解這一點。 – TheHockeyGeek 2010-03-26 14:09:04