Solved

c# how to do ftp retry

Posted on 2010-09-19
8
1,617 Views
Last Modified: 2013-12-09
hi,

i was able to create a basic ftp client that is working, but i want to make it more robust.  one of the features i want to add is retry delay (The time between each retry).  unfortunately i couldn't find a lot of materials online.  can someone help me?  thanks.
0
Comment
Question by:mmingfeilam
8 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 250 total points
ID: 33710888
you need to do something like

void DoFtpStuff()
        {
            try
            {
            }
            catch
            {
                // if exp then retry after a time period
                if (retry)
                {
                    System.Threading.Thread.Sleep(timeOut);
                    DoFtpStuff();
                }
            }
        }
0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 250 total points
ID: 33711570
//add this to your using clause
using System.Threading;
//

 private void button1_Click(object sender, EventArgs e)
        {
            int i = 0;
            int trycount = 3;
            int delayms = 2000;  //milliseconds

            while (i < trycount)
            {
                try
                {
                    DoFtp();
                    break;
                }
                catch (Exception ex)
                {
                    if (i < trycount)
                    {
                        i++;
                        Thread.Sleep(delayms );
                    }
                    else
                        throw ex;
                }
            }
        }
0
 

Author Comment

by:mmingfeilam
ID: 33712194
ok, both of your solutions seems to work.  but i don't like the fact that the main thread will be blocking during the Sleep.  the application is not scalable.  do you think i can run the DoFtp() method in a worker thread called myThread, and just call myThread.Suspend()/Resume()/Join() to create the delay?  i would think i need some kind of timer to keep track of the delay.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:starlite551
ID: 33712369
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 33712497
Using a thread will definitely make your program responsive, you can call sleep within the threads method. You dont need timers

string ErrorMsg = '';
Thread MyThread;

public void button1_Click(object sender, EventArgs e)        
        {                        
            MyThread= new Thread(new ThreadStart(DoSomeFTP));            
            MyThread.IsBackground = true;            
            MyThread.Start();        
        }  

private void DoSomeFTP()
        {
            int i = 0;
            int trycount = 3;
            int delayms = 2000;  //milliseconds

            while (i < trycount)
            {
                try
                {
                    //call the ftp methods you want
                    break;
                }
                catch (Exception ex)
                {
                    if (i < trycount)
                    {
                        i++;
                        Thread.Sleep(delayms); //this will block the thread
                    }
                    else
                        ErrorMsg = ex.Message;  //need to protect resource
            }
        }
0
 

Expert Comment

by:SanthoshVK
ID: 33714649
It is better to do the FTP functionalities in a BackgroundWorker object (which is nothing but a worker thread implementation of .NET) and let your UI subscribe fr events like OnConnect(), OnDisconnect(), OnTimeout() etc. These events are raised from the BackgroundWorker class.

If still not clear, please post back, I can give you a sample implementation.
0
 

Author Comment

by:mmingfeilam
ID: 33723294
i am not an expert at threading issues, but i believe BackgroudWorker threads are mainly for windows UI programming.  this FTP app i am building is being exposed as a web service or a windows service.  i have gotten the simple multi-threaded retry delay working thanks to ewangoya.  now i am thinking about using a ThreadPool to inprove performance.
0
 

Author Closing Comment

by:mmingfeilam
ID: 33762218
afsdws
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to start a web service from Visual Studio 2015 Pro 2 35
Hey!!!! 1 24
Automated File Transfer with FileZilla 9 69
What are the simplest ways to create a Merge Sort? 3 29
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question