2016-10-04 41 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