Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 187913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:54:32+00:00 2026-05-11T15:54:32+00:00

one my application features is tod download files from our ftp server. And of

  • 0

one my application features is tod download files from our ftp server. And of course this feature reqiures to cancel this operation (Cancel Downloading).

Now, my Download Function is as follows:

try         {             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri('ftp://' + uri + '/' + fileName));             reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;             reqFTP.UseBinary = true;             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);             reqFTP.UsePassive = true;                  response = (FtpWebResponse)reqFTP.GetResponse();                 ftpStream = response.GetResponseStream();                 _isItOutputStream = true;                 string dataLengthString = response.Headers['Content-Length'];                 int dataLength = 0;                 if (dataLengthString != null)                 {                     dataLength = Convert.ToInt32(dataLengthString);                 }                  long cl = response.ContentLength;                 int bufferSize = 4048;                 int readCount;                 byte[] buffer = new byte[bufferSize];                 readCount = ftpStream.Read(buffer, 0, bufferSize);                 outputStream = new FileStream(filePath + '\\' + fileName, FileMode.Create);                 bool first = true;                 while (readCount > 0)                 {                     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);                 }                 ftpStream.Close();                 outputStream.Close();                 response.Close();                 _isItOutputStream = false;                 return true;             }             catch (Exception ee)             {                 _downloadException = ee.Message;                 if (ftpStream != null && outputStream!=null )                     if (ftpStream.CanRead && outputStream.CanWrite)                     {                         ftpStream.Close();                         outputStream.Close();                     }                  if (response != null)                     response.Close();                  return false;             } 

Now as you can see in Catch Block you can see that i’m trying to interrupt this connection when the user clicks on Cancel button.

  • Canceling Operation Scenario:

1) Cancel button was clicked.

2) Call Function ‘DoSomeWorx()’

3) in ‘DoSomeWorx()’ do:

if (_isItOutputStream)// where here i'm trying to check if it's downloading   {    ftpStream.Close();    outputStream.Close();    response.Close();   }  if (_startCopy)// check if copying to phone   {    IsCancelled();  }  _btnDownload2PhoneThread.Abort(); // actually this operation does what i did before but for some resoans it does this but it takes time...  _btnDownload2PhoneThread.Join(); 

The Problem is when I reach any of the following statments (ftpStream.Close();outputStream.Close();response.Close();)

it throws an exception ‘File unavailable(e.g file is busy)’

and this exception affects on re-downloading operation where it sees the file busy.

so how to avoid this exception?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:54:33+00:00Added an answer on May 11, 2026 at 3:54 pm

    I’m assuming that you have a form of some sort, so your performing the download on a thread.

    What your probably better off doing is checking a ‘cancel’ flag inside your while loop.

    eg.

    while(readcount > 0 && !cancel) {    ... } 

    Then let your method gracefully cancel out.

    Secondly you should use a using statement on your streams. That means that if you throw an exception, the finally block will guarantee that your stream gets disposed (which incidentally is why you would be receiving file busy, as the stream hasn’t yet had it’s destructor run, even though your method has completed)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are building a set of features for our application. One of which is
I am working on wireframe for new feature in our application. One of the
In a .NET CF application I wrote, one of the features is to acquire
I am developing a XMPP based chat application for iOS. One of the features
I want to have a one-click excel export feature for my application. I therefore
I have a Cocoa, Document-based Mac OS X application. One feature that I have
I am making one application where i am reading mails from Lotus Notes. I
So I am developing one application on Android with multiple features. Now I am
I have an RCP application based on plugins because I started this app from
One of the best features of gwt is the edit/save/refresh development cycle. This has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.