Solved

c# how to do ftp retry

Posted on 2010-09-19
8
1,593 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
 
LVL 12

Expert Comment

by:starlite551
ID: 33712369
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
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, f…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now