我正在处理套接字编程,但我有一些关于线程机制的问题。嗯,实际上这是我头上的一个大问号。
首先,我有一个UDPListener类,我可以从网络上的某些客户端接收数据。另外,我在该类中放置了一个事件处理程序,我想在同时接收数据时调用文本框。
class udpListener
{
private static int portNumber = 9081;
private UdpClient udp = new UdpClient(portNumber);
private bool isend = false;
public volatile int CountOfReceived = 0;
public List<string> messageList = new List<string>();
public List<string> ipList = new List<string>();
public bool isClientClosed = false;
messageReceivedEventArgs mre=new messageReceivedEventArgs();
public void StartListening()
{
udp.BeginReceive(Receive, new object()); //BeginReceive must be completed by calling EndReceive method
}
private void Receive(IAsyncResult ar)
{
if (udp.Client == null)
return;
Interlocked.Increment(ref CountOfReceived);
IPEndPoint ip = new IPEndPoint(IPAddress.Any, portNumber);
byte[] bytes = udp.EndReceive(ar, ref ip); //Ends a pending asynchronous receive
if (bytes.Length > 0)
{
mre.isMessageReceived = true;
mre.message = Encoding.ASCII.GetString(bytes);
}
else
mre.isMessageReceived = false;
controlMessageReceived();
string message = Encoding.ASCII.GetString(bytes);
string[] seperator = {"|"};
string[] array = message.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
messageList.Add(array[0]);
ipList.Add(ip.Address.ToString());
Console.WriteLine(message);
StartListening();
}
public void closeSocket()
{
if (udp.Client != null)
udp.Close();
}
private void controlMessageReceived()
{
if(mre.isMessageReceived)
handler(this,mre);
}
public event EventHandler<messageReceivedEventArgs> handler;
}
public class messageReceivedEventArgs:EventArgs
{
public bool isMessageReceived { get; set; }
public string message {get;set;}
}
正如你所看到的,当bytes.length大于零时,我想是的,接收数据,现在我需要调用文本框将数据传输到文本框中。
我只是在主线程中用buttoneventhandler测试这个场景
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
networkInterface ni = new networkInterface();
udpSender UDPSender = new udpSender();
udpListener UDPListener = new udpListener();
IPAddress braddress = ni.GetBroadcastAddress(ni.GetDefaultGateway(), ni.GetSubnetMask());
UDPSender.Send(braddress, "somedatahere");
Thread.Sleep(2000);
UDPListener.StartListening();
UDPListener.handler += appendTextBox;
}
private void appendTextBox(object sender, messageReceivedEventArgs e)
{
textBox1.Text += e.message;
}
}
我会将这些消息存储到字符串列表中,在关闭套接字后,我会从该列表框中读取该数据,但是这样做非常棘手且不专业,我希望在文本框中保留这些数据收到服务器。我明确地知道我的文本框在主线程中,而appendtext方法属于Asyncronous线程,并且不允许从另一个线程访问该文本框,但我真的无法弄清楚如何做到这一点,我应该做什么获得我想要的东西。
这是我克服这个问题的解决方案,感谢@Troy Mac1ure
public partial class Form1 : Form
{
public delegate void UpdateTextCallback(string s);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
networkInterface ni = new networkInterface();
udpSender UDPSender = new udpSender();
udpListener UDPListener = new udpListener();
IPAddress braddress = ni.GetBroadcastAddress(ni.GetDefaultGateway(), ni.GetSubnetMask());
UDPSender.Send(braddress, "gr|smartrc");
Thread.Sleep(2000);
UDPListener.StartListening();
UDPListener.handler += appendTextBox;
}
private void appendTextBox(object sender, messageReceivedEventArgs e)
{
if (textBox1.InvokeRequired)
{
textBox1.Invoke(new UpdateTextCallback(addTextBox), new object[] { e.message }); // invoking itself
}
else
{
textBox1.Text += e.message;
}
}
private void addTextBox(string text)
{
textBox1.Text += text;
}
}
答案 0 :(得分:0)
您可以通过调用自身来强制在主线程上进行更新。有几种方法可以声明和执行此任务。这是一个:
public delegate void UpdateTextCallback(object sender, messageReceivedEventArgs e);
private void appendTextBox(object sender, messageReceivedEventArgs e)
{
if (textBox1.InvokeRequired)
{
listBox1.Invoke(new UpdateTextCallback(appendTextBox), new object[] { sender, e }); // invoking itself
}
else
{
textBox1.Text += e.message;
}
}