写p2p overlay的时候,碰到一个比较郁闷的问题,就是在foreach中想操作arraylist,这个后却配到了exception,我的办法是采用倒序循环:
for (int i = msgList.Count; --i > 0; )
{
message = (Message)msgList[i];
Console.WriteLine("To-ID is {0} and My-ID is {1}", message.getIdTo(), currentid);
if (message.getIdTo().Equals(currentid))
// if(string.Equals(currentid,m.getIdTo()))
{
Console.WriteLine("Find message in list!");
//find object thread, send msg to him.
byte[] byteData = new data(Command.DISPLAY, message.getMsg(), "", "").toBytes();
handler.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallBack), null);
msgList.RemoveAt(i);
//handler.Send(byteData);
}
}
本文介绍了一个在P2P Overlay网络编程中遇到的问题,即在foreach循环内直接修改ArrayList引发异常的情况及解决方案。作者通过倒序遍历的方式成功避免了异常,并实现了对消息列表的有效处理。

被折叠的 条评论
为什么被折叠?



