大家好,我使用socket异步接收UDP消息,并使用线程安全的方式把接收到的UDP消息添加到ListView里面。 目前我所知道的信息,当然不一定是正确,仅供参考:变量 text 和 委托 appendText 肯定不是null引用帮助文档的一段话:如果控件句柄尚不存在,则此方法沿控件的父级链搜索,直到它找到有窗口句柄的控件或窗体为止。如果找不到合适的句柄,此方法将引发异常。在调用过程中引发的异常将传播回调用方。(怀疑是这样的) 怀疑出现异常的代码是:{System.Object MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)}System.Windows.Forms.Control 等于 null 所以发生异常。如果是这样的话,我真不知道如何去处理。 请大家帮忙解决,谢谢 小弟万分感谢。public delegate void appendText(string[] text);public void Read_Callback(IAsyncResult ar)
{
socket.BeginReceiveFrom(state.buffer, 0, StateObject.BUFFER_SIZE, SocketFlags.None, ref endPoint, Read_Callback, state); AppendText(text);
}
else
{
socket.Close();
}
}
void AppendText(string[] text)
{
if (this.lvwLogOutput.InvokeRequired)//ListView控件
{
try
{
appendText appendtext = new appendText(AppendText);//委托
this.Invoke(appendtext, new object[] { text });//有时候这里就会出现异常。NullReferenceException
}
catch (NullReferenceException nullREP)
{
}
}
else
{
lvwLogOutput.Items.Add(new ListViewItem(new string[] { text[0], text[1], text[2], text[4] }, -1));
lvwLogOutput.TopItem = lvwLogOutput.Items[lvwLogOutput.Items.Count - 1];
}
}
{
socket.BeginReceiveFrom(state.buffer, 0, StateObject.BUFFER_SIZE, SocketFlags.None, ref endPoint, Read_Callback, state); AppendText(text);
}
else
{
socket.Close();
}
}
void AppendText(string[] text)
{
if (this.lvwLogOutput.InvokeRequired)//ListView控件
{
try
{
appendText appendtext = new appendText(AppendText);//委托
this.Invoke(appendtext, new object[] { text });//有时候这里就会出现异常。NullReferenceException
}
catch (NullReferenceException nullREP)
{
}
}
else
{
lvwLogOutput.Items.Add(new ListViewItem(new string[] { text[0], text[1], text[2], text[4] }, -1));
lvwLogOutput.TopItem = lvwLogOutput.Items[lvwLogOutput.Items.Count - 1];
}
}
this.Invoke(appendtext, new object[] { text });//有时候这里就会出现
====
应该会出现递归
就算使用异步照样出现相同的问题