|
-
Aug 10th, 2003, 08:26 PM
#1
Thread Starter
Hyperactive Member
Really Need Help Please
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...
-
Aug 11th, 2003, 09:59 AM
#2
It sounds like you are opening the login form on an alternate thread, try opening it in the UI or main thread and your problem should go away. You shouldn't need the form created asynchronously.
-
Aug 11th, 2003, 10:11 AM
#3
Thread Starter
Hyperactive Member
Originally posted by Edneeis
It sounds like you are opening the login form on an alternate thread, try opening it in the UI or main thread and your problem should go away. You shouldn't need the form created asynchronously.
Well you're correct in saying I'm opening it up a different thread. I don't have much of a choice since the originating call comes from within the Receive method that receives the incoming data from the clients.
The client has multiple forms and depending on what the user is doing, the server actually controls what forms are opened and closed on the client so calling the form load methods from the "main" thread seems impossible unless you know another way to call the main application thread?
-
Aug 11th, 2003, 10:34 AM
#4
You can call or interact with it you just can't create it in the alternate thread. So you may want to declare it globally in the main thread (with New) then just show it from the other thread. If it still gives you trouble showing it then try using the Invoke method of the form which sort of works like CallByName to call the Show method.
-
Aug 11th, 2003, 03:46 PM
#5
Thread Starter
Hyperactive Member
Originally posted by Edneeis
You can call or interact with it you just can't create it in the alternate thread. So you may want to declare it globally in the main thread (with New) then just show it from the other thread. If it still gives you trouble showing it then try using the Invoke method of the form which sort of works like CallByName to call the Show method.
Well I tried with you suggested with no success.. First I went ahead and made a "New" declaration to each form in the main thread, the results were the same, tried to .Show() and that form would lock up right after loading.
Next I even tried to Invoke the form on a new delegate, you can't invoke a form that hasn't had a .Show() done to it yet. Got an exeption.
So I did a .Show/.Hide in the main thread on application load up and it worked but it looks terrible.. The client loads up and all these forms flash open and close really quick on load up. Not very profesional looking.
Surely M$ put a way to load and open a form from a non-main thread in .Net. Does anyone here have any suggestions, sample code, anything, I'm at a loss here for ideas...
-
Aug 11th, 2003, 04:30 PM
#6
Do you have a simple implementation or sample for us to test? Or care to post what you have?
Using the things I mentioned as worked for me in the past. It could be something else causing the hang or something else about the threading that is different. If you post something I'll take a gander and try to fix it.
-
Aug 11th, 2003, 05:21 PM
#7
Thread Starter
Hyperactive Member
Originally posted by Edneeis
Do you have a simple implementation or sample for us to test? Or care to post what you have?
Using the things I mentioned as worked for me in the past. It could be something else causing the hang or something else about the threading that is different. If you post something I'll take a gander and try to fix it.
[Link Removed]
It's quite a bit of code, first run it, it will load both the server and the client. then hit "Connect", it will connect and it will load the form and you will see where it's freezing up. The problem area of the code is in the frmConnect.vb in the method Client_onReceive event and the sub LoadfrmOldLogin . Thanks in Advanced if you can help me figure out a good way to load forms from non main threads
Last edited by Hinder; Aug 11th, 2003 at 09:31 PM.
-
Aug 11th, 2003, 05:34 PM
#8
I don't know if this is a workable fix for you or not but if you show the form modally (use ShowDialog instead of Show) then everything works fine.
Exactly if you use ShowDialog then you can create the form in the other thread as well and since it is on another thread it shouldn't really halt anything so it shouldn't matter that it is shown modally.
VB Code:
Private Sub LoadfrmOldLogin()
'Show the form
Dim frm As New frmOldLogin
frm.ShowDialog()
End Sub
Last edited by Edneeis; Aug 11th, 2003 at 05:42 PM.
-
Aug 11th, 2003, 06:37 PM
#9
Thread Starter
Hyperactive Member
Originally posted by Edneeis
I don't know if this is a workable fix for you or not but if you show the form modally (use ShowDialog instead of Show) then everything works fine.
Exactly if you use ShowDialog then you can create the form in the other thread as well and since it is on another thread it shouldn't really halt anything so it shouldn't matter that it is shown modally.
VB Code:
Private Sub LoadfrmOldLogin()
'Show the form
Dim frm As New frmOldLogin
frm.ShowDialog()
End Sub
No it won't work, I already tried, it, the procedure NEEDS to return back to the starting method to recall it's receive delegate.. Using the ShowDialog() will halt that thread causing the client to quit receiving information.
I suppose I could put the ShowDialog call on it's own thread, but that would mean alot of free "locked" threads sitting being a placeholder for a open form. The client will have 13 forms all together..
I guess I'll do it that way unless you know of another workaround?
I
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|