简体   繁体   English

如何从 c# 中的类文件更新窗体 GUI for windows CE (Windows Mobile 6.5.3 ) CF 3.5 设备

[英]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我正在尝试创建一个服务器类并从客户端和接收到的消息中接收消息,我需要更新 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();
    }


  }
}

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如何更新 UI 表单 从这个课程中,我能够接收客户端消息,但 UI 无法更新我在下面尝试过

         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.请帮助我如何从此类更新 Form1 中的 responseText。

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

Try this:尝试这个:

You can use Application.DoEvents() for refreshing your GUI.您可以使用Application.DoEvents()来刷新您的 GUI。

Fore more info you can refer this link:欲了解更多信息,您可以参考此链接:

msdn link msdn 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows CE上的C#.NET 3.5 CF,在DataGrid中更改行背景色 - C# .NET 3.5 CF on Windows CE, Changing row background color in a DataGrid 如何从c#动态地从SQL Server CE [Windows mobile]运行SQL脚本文件? - How to run SQL script file from SQL Server CE [Windows mobile] from c# dynamically? 从运行WiN CE 6设备的C#.net CF 3.5向IBM Websphere MQ发送消息的最佳方法 - Best way to send message to IBM Websphere MQ from C# .net CF 3.5 running WiN CE 6 devices 如何使用.Net CF 3.5获取PDA / Windows Mobile的用户名? - How to get the UserName of the PDA/Windows Mobile using .Net CF 3.5? 如何从另一个类更新Windows窗体GUI? - how to update a windows form GUI from another class? 如何检测Windows Mobile 5设备序列号? (.NET CF 3.5) - How to detect Windows Mobile 5 Device Serial Number? (.NET CF 3.5) 如何在Windows应用程序.Net CF 3.5中显示/关闭加载表单 - How To Show / Close Loading Form In Windows Applications of .Net CF 3.5 C#Windows CE .net 3.5检查内存使用情况 - C# Windows CE .net 3.5 to checked the memory usage EndpointNotFoundException [在Windows Mobile 6.5.3 APP中] - EndpointNotFoundException [in Windows Mobile 6.5.3 APP] 如何转换/更新旧的Windows Mobile和Windows CE应用程序? - How to convert/update old Windows Mobile and Windows CE Apps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM