Click here to Skip to main content
15,886,700 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Just wondering, what is the point in invoking a method if it does not work? I have spent the past 7 hours trying to get it to work but had no luck. I dont get an error, it just simply does not work. here is my code.
C#
delegate void StringParameterDelegate(string value);

void UpdateStatus(string value)
        {
            if (InvokeRequired)
            {
                // We're not in the UI thread, so we need to call BeginInvoke
                BeginInvoke(new StringParameterDelegate(UpdateStatus), new object[] { value });
                return;
            }
            // Must be on the UI thread if we've got this far
            rtbChat.Text = value;

        }

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

I am beginning to believe that it is not possible. No matter how I set up the invoke it simply does not update my richTextBox, if I set put a messageBoz.Show in the update status it works, but it never updates the richTextBox(rtbChat).

Any help would be greatly appriciated.
Thanks in advance.
Posted
Updated 21-Mar-10 2:33am
v2

Try the following for your UpdateStatus function;
C#
private void UpdateStatus(string text)
{
   if (rtbChat.InvokeRequired)
   {
      StringParameterDelegate d = new StringParameterDelegate(UpdateStatus);
      this.Invoke(d, new object[] { text });
   }
   else
   {
      rtbChat.Text = text;
   }
}
 
Share this answer
 
v2
Or without the need for a custom delegate:
C#
void UpdateText(string value)
{
    if (InvokeRequired)
        Invoke(new MethodInvoker(delegate { UpdateText(value); }));
    else
        rtbChat.Text = value;
}


[Added] Posted a Tip about this which has attracted some alternative methods and some interesting discussion here[^].
 
Share this answer
 
v2

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