public static void DoReceive(IAsyncResult ar)
{
int Count = 0;
try
{
Count = ClientSocket.EndReceive(ar);
if (Count < 1)
{
MessageBox.Show("You have been disconnected from the server.", "My App", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
ClientText = Encoding.ASCII.GetString(ClientData);
if (ClientText.Substring(0, 14) == "LOGIN:ACCEPTED")
{
LoginForm.pbLogin.Value = 100;
LoginForm.lblProgress.Text = "Login successful!";
LoginForm.Hide();
CreateTrayIcon();
}
else if (ClientText.Substring(0, 21) == "LOGIN:FAILED PASSWORD")
MessageBox.Show("Sorry, the password you typed was incorrect. Please make sure it was typed correctly and turn caps lock off if necessary.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else if (ClientText.Substring(0, 20) == "LOGIN:FAILED ACCOUNT")
MessageBox.Show("Sorry, the account name you typed was incorrect. Please make sure it was typed correctly or that it exists as an account on the server.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
ClientSocket.BeginReceive(ClientData, 0, 1024, SocketFlags.Partial, DoReceive, null);
}
catch (Exception ex)
{
MessageBox.Show("Source: " + ex.Source + "\r\n" + "Message: " + ex.Message);
}
}
Thread or DoEvents?
I have made an client/server application in Visual C# 2005 Beta 2 and I have run into a problem. Once you have connected to the server, the client begins a data loop as shown below:
ClientSocket.BeginReceive(ClientData, 0, 1024, SocketFlags.Partial, DoReceive, null);
Once the loop has started and the client has logged in, a tray icon is created but since I need this loop to continue to receive data such as chat messages and other things for example, when I right click the tray icon nothing happens. NO MENU APPEARS!! When the loop is stopped the menu appears so I know that there IS a menu.
I've been looking into multithreading but how can I apply that to this? .NET Threads need a void with no returns, and if I put ClientSocket.BeginReceive(ClientData, 0, 1024, SocketFlags.Partial, DoReceive, null); in a void, the thread will end and the loop won't continue. I looked into DoEvents as that processes Windows messages, but where would I put it and will it work?
Thanks.
Thread. DoEvents is wholly inappropriate for this sort of thing. It merely yields to the message loop for awhile, and is a horrible holdover from VB.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement