-2

Im saw many samples of how to upload asynchronously, but not too much information on how to download from a FTP server asynchonously.

Can someone provide a small sample in c# that uses FtpWebRequest to download files asynchronously from an FPT server??

I have the full path of the files to be downloaded from the ftp server, and I need to use 1 connection to start downloading to my local machine asynchronously, so I dont need to go file by file and open/close connection for each file. Threads is not an option, because I just want (for performance reasons) 1 connection to the ftp server, and all my downloads use the existing connection (or connections??) to do the download.

I read MSDN documentation, but not sure how to use correctly the properties like ConnectionGroupName, or the build-in FtpWebRequest connection's pool, that suppose to work together with async operations to increase performance.

Thanks

user2232787
  • 135
  • 2
  • 8

1 Answers1

0

The simplest way to do this is to use code mentioned on this link and use this in a separate thread. You don't have to worry about creating asynchronous handlers in C#. Infact you could simply take the code on the link put into a method and use this :

YourClass c = new YourClass();
Thread thread = new Thread(c.method_to_download);
thread.Start();

// continue with WinForms application.

/* You can also do the following */

YourClass c = new YourClass();
c.Execute();

// where YourClass is defined as :

public class YourClass() {

    public YourClass() {}

    public void Execute() {
        // code to execute the download in a try-catch block.
    }
}

YourClass is a custom class that you may use to perfrom the download. It can raise a simple event optionally with arguments when the download is completed from the finally block. Your calling code can accept this event via an event handler and find out the result of the download.

This is just two of the ways you can deal with this, there are other ways such as using a Background thread or using asynchronous callbacks with FtpWebRequest class but they all achieve the same result.

  • Thanks for the reply. However, I need to DOWNLOAD files, not upload. I have another question.. if I use a thread to launch the DOWNLOAD in synchronous way, it will not hang my screen??? This FTP should be totally managed on the background, letting my user to continue using the WinForm application. – user2232787 Apr 16 '13 at 08:17
  • Thanks a lot for your update. I read in a book that the threads are usefull for relative small time execution operations. For long-time consuming operations (like the FTP), they recommend Tasks instead of threads. Is this true? How can I implement a task for the FTP? – user2232787 Apr 17 '13 at 10:32
  • Threads can be used for code execution regardless of the time they take. Tasks use the same concept, it is upto you what you find conceptually easier to code - I have no preference for either sasks or threads provided they do what I want. They key is to write code you understand and does the job without overly complicating the code. Please review [this](http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx) link. –  Apr 17 '13 at 16:19