與UDP

2013-07-22 22 views
1

我試圖顯示從服務器發送到客戶端的一些數據Windows窗體應用程序。客戶端腳本是一個Windows窗體應用程序,我有一個標籤名爲LABEL1其文字我試圖顯示從服務器客戶端,但label1的文本永遠不會改變在所有接收到的數據。這是什麼原因?以下是客戶端代碼。服務器腳本是一個控制檯應用程序。與UDP

現在Program.cs是空的,Form1.cs中看起來是這樣,但我仍然獲得與label1.text同樣的錯誤:

namespace WindowsFormsApplication4 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() { InitializeComponent(); } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      GetDataFromUDP(); 
     } 

     public static void SetTextForLabel(string myText) 
     { 

      label1.Text = myText; 
     } 

     private void GetDataFromUDP() 
     { 
      UdpClient subscriber = new UdpClient(8899); 
      IPAddress addr = IPAddress.Parse("230.0.0.1"); 
      subscriber.JoinMulticastGroup(addr); 
      IPEndPoint ep = null; 
      for (int i = 0; i < 10; i++) 
      { 
       byte[] pdata = subscriber.Receive(ref ep); 
       string price = Encoding.ASCII.GetString(pdata); 
       //Write data to the label 
       SetTextForLabel(price); 
      } 
      subscriber.DropMulticastGroup(addr); 
     } 
    } 
} 

裏面SetTextForLabel我得到的錯誤:

An object reference is required for the non-static field, method, or property 'WindowsFormsApplication4.Form1.label1' 



public static void SetTextForLabel(string myText) 
{ 

    label1.Text = myText; 
} 

回答

0

SetTextForLabel沒有訪問類Form1的實例的控制的靜態方法。您必須通過在Form1類中傳遞參數或聲明靜態成員來提供特定實例。

正如評論所說,這是不行的,因爲任何這樣Application.Run()代碼需要一些重構,以及開始在當前線程的應用程序。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     ConnectUDP(); 
    } 

    private void ConnectUDP() 
    { 
     UdpClient subscriber = new UdpClient(8899); 
     IPAddress addr = IPAddress.Parse("230.0.0.1"); 
     subscriber.JoinMulticastGroup(addr); 
     IPEndPoint ep = null; 
     for (int i = 0; i < 10; i++) 
     { 
      byte[] pdata = subscriber.Receive(ref ep); 
      string price = Encoding.ASCII.GetString(pdata); 
      //Write data to the label 
      label1.Text += price; 
     } 
     subscriber.DropMulticastGroup(addr); 
    } 
} 

然後在Main()

static void Main() 
{ 
    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Application.Run(new Form1()); 
} 
+0

Application.Run阻塞是不是? –

+0

是的,你是對的。您應該在窗體的構造函數,方法或按鈕單擊處理程序中運行UDP連接的代碼。如果你不想連接阻止表單,那麼這是一個不同的故事。 –

+0

我很快回復,卻沒有意識到他的完整代碼。 –

0

看起來像一個線程問題。我希望UDP調用在池中的後臺運行,因此它不會阻塞UI。只需更改方法以獲取對象,然後就可以異步調用它。然後,我剛剛得到了一個小程序,用於檢查控件是否可以直接更新,否則它只會在主UI線程中調用。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     // initialise the ConnectUDP method on a pooled thread 
     // Note: could do this from the onLoad event too 
     System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ConnectUDP)); 

    // This function just invokes the main UI thread if required 
    private static void UIThread(Control c, MethodInvoker code) 
    { 
     if (control.InvokeRequired) 
     { 
      control.BeginInvoke(code); 
      return; 
     } 
     control.Invoke(code); 
    } 

    private void ConnectUDP(object obj) 
    { 
     UdpClient subscriber = new UdpClient(8899); 
     IPAddress addr = IPAddress.Parse("230.0.0.1"); 
     subscriber.JoinMulticastGroup(addr); 
     IPEndPoint ep = null; 
     for (int i = 0; i < 10; i++) 
     { 
      byte[] pdata = subscriber.Receive(ref ep); 
      string price = Encoding.ASCII.GetString(pdata); 
      // Update the label on the main UI thread 
      UIThread(label1, delegate { 
       label1.Text += price; 
      }); 
     } 
     subscriber.DropMulticastGroup(addr); 
    } 
}