0
Reply

InvokeRequired returns false when expecting true?

Ask a question
Member1

Member1

9y
550
1
I have 2 classes form1 and handleClient class , There is one method in form1 which have code to displays data in textbox , when I called this method in same class it is working but if called in handleClient class this method doesnt works.
why this happens , please help me.
thanks .

here is my code,

in form1 class

public void AppendTxtdata(string myval)
{
    try
    {
          if (InvokeRequired)
         {
              this.Invoke(new Action<string>(AppendTxtdata), new object[] { myval });
              return;
        }
           txtdata.Text += myval;
     }
   catch { }
}
I call this method in handleClient in startCLient method in this way, 
 
public void startClient(TcpClient inClientSocket, string clineNo)
{
       this.clientSocket = inClientSocket;
       inClientSocket.NoDelay = true;
       this.clNo = clineNo;
       new Thread(GetData).Start();
}
public void GetData() // to run server //
{
       con.Open();
       this.f = new Form1();
try
{
      NetworkStream networkStream = clientSocket.GetStream();
      for (int p = 0; p >= 0; p++)
     {
         byte[] b = new byte[100];
         int k = networkStream.Read(b, 0, b.Length);
         f.AppendTxtdata("The Data from Client ( " + clNo + " ) Recieved..." +              Environment.NewLine);
      for (int i = 0; i < k; i++)
     {
        f.AppendTxtdata("" + Convert.ToChar(b[i]));
      }
      f.AppendTxtdata(Environment.NewLine);
       var j = b.Length - 1;
       while (b[j] == 0)
       {
           --j;
        }
      var temp = new byte[j + 1];
       Array.Copy(b, temp, j + 1);
        string res = System.Text.Encoding.ASCII.GetString(temp);