2014-03-25 53 views
-2

我想製作一個服務器類,並從客戶端接收消息,並從收到的消息我需要更新UI /形式,我有我需要更新 下面Form1的文本框是我的服務器類如何更新c#中的類文件的窗體GUI用於Windows CE(Windows Mobile 6.5.3)CF 3.5的設備

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形式從這節課,我能夠收到客戶端的消息但用戶界面不能更新,我試着下面

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

請幫助我如何更新Form1中的responseText來自這個類。

另外如何打開套接字接收來自客戶端的下一條消息 - 爲此我再次調用此方法recieveClientMSGS()是一種正確的方式嗎?

+0

你爲什麼要創建在插座處理方式嗎?這可能是問題的根源。創建子表單,然後在處理程序中直接顯示它。 – ctacke

回答

0

試試這個:

可以使用Application.DoEvents()刷新你的GUI。

富勒更多信息,你可以參考以下鏈接:

msdn link

+0

我試過下面的代碼,但沒有運氣 myform.responseText.Text =「ui Text Goes Here 1」; myform.responseText.Refresh(); Application.DoEvents(); –

+1

Application.DoEvents幾乎總是對不良設計的創可貼解決方案。這很有必要。 – ctacke

相關問題