views:

267

answers:

1

I have been working on a small project on and off for a while and I think I am close but it has 'issues'. The idea is to FTP a file up to a 3rd party, they process it and 5-10 minutes later they generate a result set which needs to get downloaded and processed on our side.

So the code might be a little simple, it's just something I cobbled together

            if (!Page.IsPostBack)
            {
                string baseLocation = "C:\\temp\\";
                string fn = baseLocation + "fxxxupld.inc";

                ftp = new FtpClient(FTPServer, FTPUserName, FTPPassword);
                ftp.Login();
                ftp.Upload(fn);

                AsyncCallback callback = new AsyncCallback(CloseConnection);

                ftp.ChangeDir("results");
                string[] Files = ftp.GetBloombergUploadList();
                int CHigh = Files.GetUpperBound(0);
                String LatestFile = CheckForNewFile(CHigh, ftp);
                ftp.BeginDownload(LatestFile, "c:\\temp\\Results.txt", callback);
            }

        private static string CheckForNewFile(int CHigh,FtpClient ftp)
        {
            int NHigh = 0;
            string LatestFile = "";
            while (CHigh >= NHigh)
            {
                string[] Files = ftp.GetBloombergUploadList();
                NHigh = Files.GetUpperBound(0);
                LatestFile = Files[NHigh-1].ToString();
                Thread.Sleep(3000);
            }
            return LatestFile;
        }

        private void CloseConnection(IAsyncResult result)
        {
            Debug.WriteLine("File downloaded: " + result.IsCompleted.ToString());
            if (ftp != null) ftp.Close();
            ftp = null;
        } 

Pretty simple, push the file up, get a list of the currently processed files, wait and check every so often and when a new file shows up, download it.

Before I added the AsyncCallback in, the process worked fine except the user couldn't do anything until the page returned and I couldn't display any kind of progress indicator, etc. After adding in the AsyncCallback all of a sudden the UPLOAD part is spamming the remote end and I managed to crash them (or at least our particular connection).

Any thoughts on it would be appreciated... needly to say I sort of need to get the process fixed right because our traders get a little upset when they can't work and Bloomberg gets a little upset when they have to spend 3+ hours trying to resolve it :(

thanks!

+1  A: 

I'm guesing your top chunk of code is in the Page_Load handler? And the line

 ftp.Upload(fn);

Is causing the problem? If so I'd say your users were reloading the page, so this is getting called much more often than before.

You are on the right lines to look at async patterns, but it sound like you need to guard against the case where people refresh the page too much.

Steve Haigh
right now I'm testing it in a blank page, I'm just using the Page_Load handler to kick it off, there is nothing to refresh. Maybe I'll take it out of there and kick it off someplace else, just to rule it out. thanks.
SomeMiscGuy
If you can reproduce the issue with a really simple example it might be worth editing your code sample.
Steve Haigh