2016-10-04 63 views
0

我真的不熟悉C#與WPF,我試圖編寫一個程序來顯示實時UDP消息(基本上只是一個值),但是我不能將UDP用我的c#代碼爲WPF監聽代碼,如果我像使用UDP監聽函數一樣,它沒有彈出窗口,任何想法如何操作?萬分感激!C#WPF實時UDP消息

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

//UDP 
using System.Net; 
using System.Net.Sockets; 


namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     string Rawdata_str1 = "0"; 

     public MainWindow() 
     { 
      InitializeComponent(); 
      UDP_listening_PI1(); 
      X_values.DataContext = new X_Y() { X = Rawdata_str1 }; 
      Y_values.DataContext = new X_Y() { Y = Rawdata_str1 }; 
     } 

     public void UDP_listening_PI1() 
     { 
      UdpClient listener = new UdpClient(48911); 
      IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, (48911)); 
      bool done = false; 
      try 
      { 
       while (!done) 
       { 
        byte[] pdata = listener.Receive(ref groupEP); 
        string price = Encoding.ASCII.GetString(pdata); 
        int Rawdata1 = int.Parse(price); 
        Rawdata_str1 = Rawdata1.ToString(); 
       } 
      } 

      finally 
      { 
       listener.Close(); 
      } 
     } 
    } 

    public class X_Y 
    { 
     public string X { get; set; } 
     public string Y { get; set; } 
    } 

} 

回答

0

你正在收聽的主UI線程的UDP端口,
這就是爲什麼你沒有看到,當你作出這一呼籲
作爲UI線程控制流是主窗口被UDP_listening_PI1方法阻止。

您需要偵聽不同後臺線程中的UDP端口,然後
會在需要時將消息泵送回主UI線程。

你可能需要像下面這樣的東西。

private readonly Dispatcher _uiDispatcher; 
    public MainWindow() 
    { 
     InitializeComponent(); 
     _uiDispatcher = Dispatcher.CurrentDispatcher; 
     var dataContext = new X_Y(); 
     X_values.DataContext = dataContext; 
     Y_values.DataContext = dataContext; 
     Task.Factory.StartNew(UDP_listening_PI1); 
    } 

    public void UDP_listening_PI1() 
    { 
     UdpClient listener = new UdpClient(48911); 
     IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, (48911)); 
     bool done = false; 
     try 
     { 
      while (!done) 
      { 
       byte[] pdata = listener.Receive(ref groupEP); 
       string price = Encoding.ASCII.GetString(pdata); 
       int Rawdata1 = int.Parse(price); 
       Rawdata_str1 = Rawdata1.ToString(); 
       UpdateXY(Rawdata_str1); 
      } 
     } 

     finally 
     { 
      listener.Close(); 
     } 
    } 

    private void UpdateXY(string rawData) 
    { 
     if (!_uiDispatcher.CheckAccess()) 
     { 
      _uiDispatcher.BeginInvoke(DispatcherPriority.Normal,() => { UpdateXY(rawData); }); 
      return; 
     } 
     dataContext.X = rawData; 
     dataContext.Y = rawData; 
     //Need to raise property changed event. 
    } 

您還需要提高INotifyPropertyChanged當你設置值X & Y. Modidy X & Y的制定者和引發事件。

+0

謝謝!它確實工作! – tedhan