.net网页中的AsyncCallback跑掉

发布于 2024-07-16 23:31:28 字数 1836 浏览 6 评论 0原文

我断断续续地从事一个小项目有一段时间了,我想我已经接近了,但它有“问题”。 我们的想法是将文件通过 FTP 传输给第三方,他们对其进行处理,5-10 分钟后他们会生成一个结果集,需要在我们这边下载和处理。

所以代码可能有点简单,这只是我拼凑在一起的东西

            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;
        } 

非常简单,将文件向上推,获取当前处理的文件的列表,等待并经常检查,当新文件出现时,下载它。

在我添加 AsyncCallback 之前,该过程工作正常,除了用户在页面返回之前无法执行任何操作,并且我无法显示任何类型的进度指示器等。在突然添加 AsyncCallback 后,上传部分是向远程端发送垃圾邮件,我设法使它们崩溃(或者至少使我们的特定连接崩溃)。

对此的任何想法将不胜感激......需要说的是,我需要正确修复流程,因为我们的交易员在无法工作时会有点沮丧,而彭博社在必须花费 3 个多小时时会有点沮丧试图解决它:(

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回旧景 2024-07-23 23:31:28

我猜您的顶部代码块位于 Page_Load 处理程序中? 线路

 ftp.Upload(fn);

引起了问题吗? 如果是这样,我会说您的用户正在重新加载页面,因此调用此方法的频率比以前要高得多。

您正在正确地查看异步模式,但听起来您需要防止人们过度刷新页面的情况。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文