Solved

c# how to do ftp retry

Posted on 2010-09-19
8
1,596 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Cloud file services can fill many different roles for your business. Often, the use of cloud file services begins with employees using consumer products, like Dropbox, to share files with customers and each other. While sync-and-share can be an effe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
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…

816 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

10 Experts available now in Live!

Get 1:1 Help Now