Solved

c# how to do ftp retry

Posted on 2010-09-19
8
1,581 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Outsource Your Fax Infrastructure to the Cloud (And come out looking like an IT Hero!) Relative to the many demands on today’s IT teams, spending capital, time and resources to maintain physical fax servers and infrastructure is not a high priority.
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.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

747 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

11 Experts available now in Live!

Get 1:1 Help Now