FtpWebRequest 异步调用问题
直接上代码了,以下代码,
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Abc();
Abc();
Abc();
}
private void Abc()
{
FtpWebRequest ftpRequest = null;
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://xxx.xxx.xxx.xxx/storage/7788"));
ftpRequest.UseBinary = true;
ftpRequest.Credentials = new NetworkCredential("xxx", "xxx");
ftpRequest.Method = "LIST";// WebRequestMethods.Ftp.GetFileSize;
ftpRequest.Timeout = 5000;
ftpRequest.ReadWriteTimeout = 5000;
ftpRequest.BeginGetResponse(new AsyncCallback(EndGetResponseLengthCallback), null);
MessageBox.Show("Q");
}
catch (Exception ex)
{
if (ftpRequest != null)
{
ftpRequest.Abort();
}
}
finally
{
Console.WriteLine("Step1 Completed");
}
}
private void EndGetResponseLengthCallback(IAsyncResult ar)
{
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => {
MessageBox.Show("A");
}));
FtpWebResponse ftpResponse = null;
try
{
ftpResponse = (FtpWebResponse)ftpRequest.EndGetResponse(ar);
string fileUploadLog = " 服务返回状态码: " + ftpResponse.StatusCode + " 状态描述信息: " + ftpResponse.StatusDescription;
Console.WriteLine(fileUploadLog);
}
catch (Exception ex)
{
if (ftpRequest != null)
{
ftpRequest.Abort();
}
}
finally
{
if (ftpRequest != null)
{
ftpRequest.Abort();
}
}
}
}
上述代码相信是一个比较简单的获取ftp目录列表的代码,网上很多例子都是这么写的,但是,我实际测试过程中发现,在我断开网络连接的时候,启动程序,在主线程中连续三次调用Abc()方法,只有2次会运行到异步回调函数(EndGetResponseLengthCallback),第三次异步调用后一直没有进回调函数,请大神忙帮助一下,看看这代码如何才能让第三次及以后的异步调用都进回调函数。