如何修复System.dll中发生的类型为“System.InvalidOperationException”的未处理异常 [英] How can fix the An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll
问题描述
public void Start()
{
_listener = new TcpListener(IPAddress.Any, 21);
_listener.Start();
_listener.BeginAcceptTcpClient(HandleAcceptTcpClient, _listener);
}
public void Stop()
{
if (_listener != null)
{
_listener.Stop();
}
}
private void HandleAcceptTcpClient(IAsyncResult result)
{
TcpClient client = _listener.EndAcceptTcpClient(result);
_listener.BeginAcceptTcpClient(HandleAcceptTcpClient, _listener);
Iplist.Add(((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
lstBox1.Items.Add(Iplist[0]);
}
i希望得到哪个客户端的ip连接我的ftp服务器并将其写入列表框。
我试图将ips添加到array.There没有问题,但是我想将这个数组项添加到listbox我得到了这个异常类型'系统未处理的异常.InvalidOperationException'发生在System.dll,我无法解决它。我怎么能修复它?
i want to get which client's ip connected my ftp server and i write it to listbox.
I tried to add ips to array.There is no problem, However i want to add this arrays item to listbox I got this exception "An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll" and i cant fix it.How can i fix it?
推荐答案
试试这个:
private void HandleAcceptTcpClient(IAsyncResult result)
{
// put a break-point here and observe what happens
TcpClient client = _listener.EndAcceptTcpClient(result);
_listener.BeginAcceptTcpClient(HandleAcceptTcpClient, _listener);
IPEndPoint ipEnd = client.Client.RemoteEndPoint as IPEndPoint;
// put a break-point here and observe what happens
if (ipEnd != null)
{
Iplist.Insert(0, ipEnd.Address.ToString());
lstBox1.Items.Add(Iplist[0]);
}
else
{
throw new ArgumentNullException("ipEnd is null");
}
}
如图所示放置断点,然后单步执行代码以查找错误发生的位置,以及错误发生前关键变量的值。
Put break-points as shown, and single step through the code looking for where the error occurs, and what the values of key variables are before the error occurs.
由于以下原因而发生:
1.如果不指定数据源或服务器,则无法打开连接。或者
2.连接已经打开。
检查
It occurs because of following reasons:
1. Cannot open a connection without specifying a data source or server. or
2. The connection is already open.
check it
这篇关于如何修复System.dll中发生的类型为“System.InvalidOperationException”的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!