Sign in to follow this  
Kryptus

Thread or DoEvents?

Recommended Posts

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);
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);
            }
        }

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this