Solved

c# how to do ftp retry

Posted on 2010-09-19
8
1,634 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Ephraim Wangoya
Ephraim Wangoya 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:starlite551
ID: 33712369
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
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…

763 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