I am using Asynchronous sockets in a C# application but a minor problem has poped up. When I receive data, i need to show a form and display the data in it. When i do this (using either the Show() method or the Visible=true property) the application crashes. It looks like it the form entered in an infinite loop (the cursor turns to an hour glass) - the rest of the application remains usable, except for that new form.
I have temporarily fixed this by using the ShowDialog() mothod instead of Show() [which for some odd reason doesn't block the program, even though it should]. This made me think that the cause is because the new form is executing in another thread's context, but i know it shouldn't do this so i am not sure 100% :(
Does any know of a solution to this?!
this is the code i use:
string serv_rply = new string(System.Text.Encoding.ASCII.GetChars(cli.Recv()));
MessageBox.Show(serv_rply);
NetProtocolCmd npc = NetProtocolCmd.LoadFromXml(serv_rply);
if (npc.Command == NPCommands.npcLoginStatus) {
/* check the login status, but for now assume success */
if (npc.isLoginSuccessful()) {
frmNotify n = new frmNotify("hello", "world", 6000);
n.Top = 50;
n.Left = 50;
n.Height = 150;
n.Width = 150;
//n.Show();
n.ShowDialog();
}
}
serv_rply is the string that holds the data received (which is xml)
npc is a deserialized object from the data received (the sender serializes the object into XML and sends the xml)
Question
georgevella
I am using Asynchronous sockets in a C# application but a minor problem has poped up. When I receive data, i need to show a form and display the data in it. When i do this (using either the Show() method or the Visible=true property) the application crashes. It looks like it the form entered in an infinite loop (the cursor turns to an hour glass) - the rest of the application remains usable, except for that new form.
I have temporarily fixed this by using the ShowDialog() mothod instead of Show() [which for some odd reason doesn't block the program, even though it should]. This made me think that the cause is because the new form is executing in another thread's context, but i know it shouldn't do this so i am not sure 100% :(
Does any know of a solution to this?!
this is the code i use:
string serv_rply = new string(System.Text.Encoding.ASCII.GetChars(cli.Recv())); MessageBox.Show(serv_rply); NetProtocolCmd npc = NetProtocolCmd.LoadFromXml(serv_rply); if (npc.Command == NPCommands.npcLoginStatus) { /* check the login status, but for now assume success */ if (npc.isLoginSuccessful()) { frmNotify n = new frmNotify("hello", "world", 6000); n.Top = 50; n.Left = 50; n.Height = 150; n.Width = 150; //n.Show(); n.ShowDialog(); } }serv_rply is the string that holds the data received (which is xml)
npc is a deserialized object from the data received (the sender serializes the object into XML and sends the xml)
Link to comment
Share on other sites
2 answers to this question
Recommended Posts