Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 3 classes form1.cs and handleClient.cs and GlobalVariable.cs , There is one method in form1.cs which have code to display data in textbox , when called this method from form1.cs itself, it is working fine but if called in handleClient.cs this method doesnt works. I assign data to be display in textbox to static string variable "somedata" declared in GloabalVariable class. and data is display in textbox but after closing connection objects, lets say closing threads of clients. But i want to display data at runtime, How to make this possible ? Please help.

thanks .

here is my code,

in GlobalVariable class

C#
class GlobalVariable
  {
      public static string SomeData = "";
  }


in form1 class
C#
namespace connectSuccess
{
    public partial class Form1 : Form
    {
        private handleClient handleClient;
        private bool IsRunning = false;
        TcpListener serverSocket;
        TcpClient clientSocket;
      
        public Form1()
        {
            InitializeComponent();
        }

        public Form1(handleClient handleClient)
        {
            // TODO: Complete member initialization
            this.handleClient = handleClient;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string ipaddre = getip();
            IPAddress iP = IPAddress.Parse(ipaddre);
            AppendTxtip(iP.ToString());
        }

        private void btnstart_Click(object sender, EventArgs e)
        {
            new Thread(startServer).Start();
        }

        public  void startServer()
        {
            try
            {
                int port = Convert.ToInt32(txtport.Text);
                string ipaddre = getip();
                IPAddress iP = IPAddress.Parse(ipaddre);
                // AppendTxtip(iP.ToString());
                serverSocket = new TcpListener(iP, port);
                clientSocket = default(TcpClient);
                int counter = 0;
                serverSocket.Start();
               AppendTxtdata("Server Started , Waiting for Client Connection" + Environment.NewLine);

                IsRunning = true;

                while (IsRunning)
                {
                    counter += 1;
                    clientSocket = serverSocket.AcceptTcpClient();
                    AppendTxtdata(" >> " + "Client No:" + Convert.ToString(counter) + " Connected-- IP --" + clientSocket.Client.RemoteEndPoint.ToString() + "");
                    AppendTxtdata(Environment.NewLine);
                    handleClient client = new handleClient();
                    client.startClient(clientSocket, Convert.ToString(counter));
                    // break;
                }
               
            }
            catch { }
            finally { }
        }

        public void StopServer()
        {
            IsRunning = false;
            
        }

 public void AppendTxtdata(string myval)
        {
            try
            {
                if (InvokeRequired)
                {
                        this.Invoke(new Action<string>(AppendTxtdata), new object[] { myval });
                        return;
                }
                
                GlobalVariable.SomeData += myval;
                txtdata.Text = GlobalVariable.SomeData;
            }
            catch 
            { }
        }


I am calling this method in handleClient.cs in startCLient method in this way,

C#
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();
          Form1 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);
Posted
Updated 11-Aug-15 20:03pm
v4

1 solution

Well...it probably does work, but you can't see it.
And you can't see it because the data goes to your new instance of the form:
C#
        public void GetData() // to run server //
        {
            con.Open();
            Form1 f = new Form1();
...
                   f.AppendTxtdata("The Data from Client ( " + clNo + " ) Recieved..." + Environment.NewLine);
...
Which you don't display, instead of going to the already running instance of teh form you can see.

See here: Transferring information between two forms, Part 2: Child to Parent[^] it refers to forms, but it's the same process when the child is a class.
 
Share this answer
 
Comments
OriginalGriff 18-Jul-15 4:22am    
Google:
https://www.google.co.uk/search?q=the+message+filters+indicate+that+the+application+is+busy&oq=the+message+filters+indicate+that+the+application+is+busy&aqs=chrome..69i57&sourceid=chrome&es_sm=93&ie=UTF-8
OriginalGriff 18-Jul-15 4:36am    
We don't know enough about the rest of your code to even start answering that: we don't even know what *you* are doing at the time it occurs, much less what your code is doing!
You are going to have to look at your code rather carefully and work out what happens when before we could even start to answer that for you.
OriginalGriff 18-Jul-15 5:29am    
"We don't know enough about the rest of your code..."
Still applies! :laugh:

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