I'm having a problem in my application and I hope someone can help. Here's the problem.. I have built a server/client application and they both are running on a Asyncronous socket set up. The server is Console based but my Client has many forms. The actual socket code on the client is in a Class. When a user logs in, he sends his name/pass/client version, then the server server checks his this information, it then sends a packet to the client and the client then hides the login form and opens up a new form.
For some reason when the new form loads, it freezes. I'm almost 100% sure this is a threading issue. If you understand how Asyncronous sockets work, basically The Receive thread just continues to recall itself over and over in a new memory address through Asyncronous Calls. Here's some code showing the Receive Call and the methods that handle the data..
This is the actual Receive Delegate that monitors incoming data
Code:Private Sub ReceiveLock(ByVal ar As IAsyncResult) Dim State As SocketState = CType(ar.AsyncState, SocketState) Dim Handler As Socket = State.Socket Dim ByteCount As Int32 Try 'Get the byte count ByteCount = Handler.EndReceive(ar) Catch ex As Exception CloseSocket() End Try Try 'Get the data If ByteCount < 1 Then CloseSocket("Connection To Com-Link Terminated") Exit Sub Else 'Handle the incoming data State.SB.Append(Encoding.ASCII.GetString(State.RBuff, 0, ByteCount)) Dim Msg As String = State.SB.ToString() If Msg.EndsWith(Chr(0)) Then DataHandler(Msg.Substring(0, Msg.Length - 1)) State.SB = Nothing State.SB = New StringBuilder End If Handler.BeginReceive(State.RBuff, 0, State.PacketSize, SocketFlags.None, New AsyncCallback(AddressOf ReceiveLock), State) End If Catch ex As Exception CloseSocket("Connection To Com-Link Terminated") End Try
This is the code that handles the data being processes and checks for what kind of packet being received
Code:Private Sub DataHandler(ByVal inMsg As String) 'Process the incoming data Select Case Convert.ToInt32(inMsg.Substring(0, 3)) Case Is = MD.CloseClient CloseSocket(inMsg.Substring(3)) Case Is = MD.OpenOldLogin OpenOldLogin() Case Is = MD.OpenNewLogin End Select End Sub
This is the method that is supposed to open the form that I have pre declares in this class.
Code:Private Sub OpenOldLogin() 'Hide the current form ref_frmLogin.Hide 'Load the new form ref_frmOldLogin = New frmOldLogin(Me) ref_frmOldLogin.Show() 'This is where it locks up. The re_frmOldLogin will load, but then 'a hourglass mouse icon will stay busy and the form is frozen. End Sub
The above block is the method that opens the new form.. As I mentioned, the form loads, and then freezes and your mouse cursor stays a hourglass. Any help would be appretiated as this is really putting a halt on things...




Reply With Quote