如何从c#中的类文件更新窗体GUI,用于Windows CE(Windows Mobile 6.5.3)具有CF 3.5的设备

时间:2014-03-25 11:59:05

标签: c# .net windows-ce .net-cf-3.5

我正在尝试创建一个服务器类并从客户端接收消息,并从收到的消息我需要更新UI /表单,我有form1中的文本框,我需要更新 下面是我的服务器类

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


  }
}

如何更新UI表单从这个类,我能够接收客户端消息但是UI无法更新我试过下面

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

请帮助我如何更新Form1中的responseText来自此类。

另外如何打开套接字以接收来自客户端的下一条消息 - 因为我再次调用此方法 - recieveClientMSGS()是一种正确的方法吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

您可以使用Application.DoEvents()来刷新GUI。

有关详细信息,请参阅此链接:

msdn link