Хорошо, сначала поставьте закрытие в блок, наконец.
Поэтому убедитесь, что ваш код подобен этому, чтобы гарантировать, что потоки закрыты, несмотря ни на что.
try
{
//Your existing logic
//Don't need to close the streams/etc
}
catch
{
//Just do what you need to with the exception
}
finally
{
//This is always called
if (response != null)
response.Close();
if(outputStream != null)
outputStream.Close();
if(ftpStream != null)
ftpStream.Close();
}
Обернуть эту строку в попытку поймать
ftpStream = response.GetResponseStream();
Это должно быть хорошо, пока эти вызовы Close вызываются, где ваша логика отмены? Я ожидаю увидеть флаг в цикле, который устанавливается, когда пользователь нажимает кнопку. например,
while (readCount > 0 && !_shouldExit)
{
outputStream.Write(buffer, 0, readCount);
_actualDownloaded += readCount;
if (this.InvokeRequired)
{
ProgressBarDel _progressDel = new ProgressBarDel(ProgressBar);
this.Invoke(_progressDel, new object[] { _actualDownloaded, first });
}
first = false;
readCount = ftpStream.Read(buffer, 0, bufferSize);
}