tedhan
tedhan

Reputation: 73

C# WPF real-time UDP message

I'm really new to C# with WPF, I'm trying to write a program to display real-time UDP message (basically just a value), however I can't incorporate the UDP listening code with my c# code for WPF, it has no window pops up if I use UDP listening function as I did, any idea how to do it? Really appreciate!

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; }
    }

}

Upvotes: 2

Views: 3904

Answers (1)

Vignesh.N
Vignesh.N

Reputation: 2666

You are listening to the UDP port in the main UI thread,
that is why you don't see the main window when you are making that call
as the control flow on the UI thread is blocked by the UDP_listening_PI1 method.

You need to listen to the UDP port in a different background thread and then,
pump back the message to the main UI thread if and when required.

You probably need something like below.

    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.
    }

You also need to raise the INotifyPropertyChanged when you set values to X & Y. Modidy the setters of X & Y and raise the event.

Upvotes: 2

Related Questions