简体   繁体   中英

How to update the form GUI from a class file in c# for windows CE (Windows Mobile 6.5.3 ) Devices with CF 3.5

I am trying to make a server class and receive messages from client and from the received msgs i need to update the UI/form , i have text box in the form1 which i need to be updated Below is my server class

  using System;
  using System.Linq;
  using System.Collections.Generic;
  using System.Text;
  using System.Windows.Forms;
  using System.Threading;
  //
 /*   Server Program    */
  using System.Net;
  using System.Net.Sockets;

 using System.ComponentModel;

 namespace TCPConnectionExample
{

 public class serv : Component
 {
    public String clientResponse = "";
    public TcpListener myList;
    public Socket s;
    public IPAddress ipAd;
    private Form1 form;

  public serv  (Form1 form)
     {
           this.form = form;
     }
  public serv()
  {

  }


    public  void Server()
    {          
        try
        {
            //IPAddress ipAd = IPAddress.Parse("10.20.20.146");
            //IPAddress ipAd = IPAddress.Parse("192.168.1.122");
            IPHostEntry host;
            string localIP = "?";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                }
            }


            // use local m/c IP address, and 
            // use the same in the client

            IPAddress ipAd = IPAddress.Parse(localIP);
            System.Diagnostics.Debug.WriteLine("Success: ip of server is set");
            /* Initializes the Listener */
            //TcpListener myList = new TcpListener(ipAd, 8001);

            TcpListener myList = new TcpListener(ipAd, 1024);
            /* Start Listeneting at the specified port */
            myList.Start();


            System.Diagnostics.Debug.WriteLine("The server is running at port 1024...and server  ip is " + ipAd);
            System.Diagnostics.Debug.WriteLine("The local End point is  :" +
                              myList.LocalEndpoint);
            System.Diagnostics.Debug.WriteLine("Waiting for a connection.....");               

             s = myList.AcceptSocket();
            Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
            System.Diagnostics.Debug.WriteLine("Connection accepted from " + s.RemoteEndPoint);

            recieveClientMSGS();

               /* clean up &  TO Close the TCP Socket */
               // s.Close();
              // myList.Stop();

           // Form1 form1 = new Form1();
           // form1.TCPResponse.Text = clientResponse;// add what the client send to to the  richtextbox 
            // TCPResponse.Refresh();

        }

        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
            System.Diagnostics.Debug.WriteLine("Error..... " + e.StackTrace);
        }
    }

    public void recieveClientMSGS()
    {
        System.Diagnostics.Debug.WriteLine("Waitng for client msgs...");
        // From below Receieves the response  from the client here aftr the connection is established
        byte[] b = new byte[100];
        int k = s.Receive(b);
        Console.WriteLine("Recieved...");
        System.Diagnostics.Debug.WriteLine("Recieved...");
        for (int i = 0; i < k; i++)
        {
            System.Diagnostics.Debug.Write(Convert.ToChar(b[i]));
            clientResponse = clientResponse + Convert.ToChar(b[i]).ToString();
        }

        System.Diagnostics.Debug.WriteLine("\n client send: " + clientResponse);

        ASCIIEncoding asen = new ASCIIEncoding();
        s.Send(asen.GetBytes("The string was recieved by the server."));
        Console.WriteLine("\nSent Acknowledgement");
        System.Diagnostics.Debug.WriteLine("\nSent Acknowledgement");           

       // form1.TCPResponse.Text = clientResponse;// add what the client send to to the  richtextbox 
       // form1.TCPResponse.Refresh();
       // form = new Form1();

        this.form = new Form1();
        //this.form.responseText.Text = "ui Text Goes Here 1";
        //this.form.responseText.Refresh();

       // this.form.AppendText("Hello World");      


       // this.form.SetSomething("dsfsdgvdsg sgvsd"); // USED TO UPDATE THE UI
        this.form.UIThread(() => this.form.responseText.Text = "ui Text Goes Here");
        this.form.UIThread(() => this.form.responseText.Refresh());
        recieveClientMSGS();
    }


  }
}

How to update the UI form From this class , I am able to receive the client messages but the UI Not able to update i tried below

         this.form = new Form1();
        this.form.responseText.Text = "ui Text Goes Here 1";
        this.form.responseText.Refresh();

Please help me how to update the the responseText in the Form1 From this class.

Also how to open the socket to recieve next message from client --for that i calls this method again- recieveClientMSGS() is that a correct way ?

Try this:

You can use Application.DoEvents() for refreshing your GUI.

Fore more info you can refer this link:

msdn link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM