C# 非阻塞UDP客户端接收线程安全调用

C# 非阻塞UDP客户端接收线程安全调用,c#,.net,network-programming,udp,nonblocking,C#,.net,Network Programming,Udp,Nonblocking,多年来我一直在寻找解决这个问题的办法。我试过很多东西,包括BeginReceive(),但都没有用。必须有办法做到这一点,使UDP客户端接收非阻塞和线程安全的调用。我正在尝试接收消息并将其写入富文本框 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System

多年来我一直在寻找解决这个问题的办法。我试过很多东西,包括BeginReceive(),但都没有用。必须有办法做到这一点,使UDP客户端接收非阻塞和线程安全的调用。我正在尝试接收消息并将其写入富文本框

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace Chat
{
    public partial class Form1 : Form
    {
        public bool contfunctioning = true; public bool changedsplay = false;
        public string enteredchats, msg, chatstring, Chats;

        UdpClient subscriber = new UdpClient(8899);
        UdpClient publisher = new UdpClient("230.0.0.100", 8898);


        public Form1()
        {
            InitializeComponent();
        }


        private void btnConnect_Click(object sender, EventArgs e)
        {

            string ConnectIPAddress;
            ConnectIPAddress = txtboxIP.Text;
            IPAddress addr = IPAddress.Parse(ConnectIPAddress);

            MessageBox.Show("Subscribing to chat server on " + ConnectIPAddress + ".", ConnectIPAddress);

            EndPoint ep = null;

            // This is where The UDPClient subscriber needs to Begin.Receive()


        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            enteredchats = txtboxUsr.Text + " " + txtboxentertxt.Text;
            txtboxentertxt.Clear();

            msg = String.Format(enteredchats);
            sdata = Encoding.ASCII.GetBytes(msg);
            publisher.Send(sdata, sdata.Length);
        }

    }
}
足够的代码,是吗? 提前感谢您的回复。

我发现使用计时器()可以避免这个问题

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Diagnostics;


namespace Chat
{
    public partial class Form1 : Form
    {


        public bool contfunctioning = true; public bool changedsplay = false; public bool setupntwrk = false;
        public string enteredchats, msg, chatstring, Chats, lastchatstring;

        UdpClient subscriber = new UdpClient(8899);
        UdpClient publisher = new UdpClient("230.0.0.100", 8898);

        System.Windows.Forms.Timer timer1use = new System.Windows.Forms.Timer();



        public Form1()
        {
            InitializeComponent();
        }



        private void btnConnect_Click(object sender, EventArgs e)
        {
            Thread rcvchats = new Thread(ReceiveChats);
            rcvchats.Start();

            timer1use.Interval = 1000;
            timer1use.Start();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            enteredchats = txtboxUsr.Text + ": " + txtboxentertxt.Text;
            txtboxentertxt.Clear();

            msg = String.Format(enteredchats);
            byte[] sdata = Encoding.ASCII.GetBytes(msg);
            publisher.Send(sdata, sdata.Length);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (chatstring != lastchatstring)
            dsplay.AppendText("\r\n" + chatstring);

            lastchatstring = chatstring;


        }

        public void ReceiveChats()
        {
            while (true)
            {
                if (setupntwrk == false)
                {
                    string ConnectIPAddress;
                    ConnectIPAddress = txtboxIP.Text;
                    IPAddress addr = IPAddress.Parse(ConnectIPAddress);
                    MessageBox.Show("Subscribing to chat server on " + ConnectIPAddress + ".", ConnectIPAddress);
                    subscriber.JoinMulticastGroup(addr);

                    setupntwrk = true;
                }

                IPEndPoint ep = null;
                chatstring = Encoding.ASCII.GetString(subscriber.Receive(ref ep));

                Thread.Sleep(1000);
            }

        }

        private void btnHost_Click(object sender, EventArgs e)
        {
            Process starthost = new Process();
            starthost.StartInfo.FileName = "C:\\ChatServ.exe";
            starthost.Start();

        }
    }
}

您最近是如何尝试开始的?如果你正确使用它,它是线程安全的。你到底有什么问题?在这里使用BeginReceive可以很好地工作。是我还是代码段在某处包含BeginReceive?它不包含BeginReceive(),正如我在问题中所述,我尝试过它,但它不起作用。如果有人能给我一个答案,告诉我如何在这种情况下做到这一点,我将不胜感激。为什么投反对票?回答我自己的问题是合法的,事实上,它正好有一个徽章。