Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
EDIT:

Okay, thanks to John Simmons I kinda sorta got somewhere. Instead of it simply not working, now it gives me this error when I try to send something from the server.

"Invoke or BeginInvoke cannot be called on a control until the window handle has been created."

I guess this has something to do with the form not being completely loaded before it invokes the richTextBox, but how can I make it wait for the form to load the windows handle before proceeding?

Thanks in advance.
Posted
Updated 16-Mar-10 10:38am
v10

This article[^] should help you out then.

:)
 
Share this answer
 
Your Invoke implementation is correct, so the problem must be somewhere else.

Have you checked this in the debugger?

Is the line control.Text = text; being executed in the SetText method?

In your call to SetText( rtbChat, content ); do the parameters hold correct values?

Nick
 
Share this answer
 
I probably wouldn't invoke myself like you're doing.

And, since you *know* the actual type of control in question, why not pass that type to the delegate, or just the text (since you can safely assume that the actual control itself exists)?

You're adding obscurity for the (intended) sake of convenience, and muddying the picture as a result.

public delegate void DelegateTextSetter;
public DelegateTextSetter TextSetter = new DelegateTextSetter(SetText);

public void SetText(string text)
{
    myRichTextControl.Text = text;
}

public void OnReceive(IAsyncResult ar)
{
    String content = String.Empty;
    StateObject state = (StateObject)ar.AsyncState;
    Socket handler = state.workSocket;
    int bytesRead;
    if (handler.Connected)
    {
        // Read data from the client socket.
        try
        {
            bytesRead = handler.EndReceive(ar);
            if (bytesRead > 0)
            {
                state.sb.Remove(0, state.sb.Length);
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
                // Display Text in Rich Text Box
                content = state.sb.ToString();

                Invoke(TextSetter, content);

                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(OnReceive), state);
            }
        }
        catch (SocketException socketException)
        {
            if (socketException.ErrorCode == 10054 || ((socketException.ErrorCode != 10004) && (socketException.ErrorCode != 10053)))
            {
                handler.Close();
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900