.net网页中的AsyncCallback跑掉
我断断续续地从事一个小项目有一段时间了,我想我已经接近了,但它有“问题”。 我们的想法是将文件通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜您的顶部代码块位于
Page_Load
处理程序中? 线路引起了问题吗? 如果是这样,我会说您的用户正在重新加载页面,因此调用此方法的频率比以前要高得多。
您正在正确地查看异步模式,但听起来您需要防止人们过度刷新页面的情况。
I'm guesing your top chunk of code is in the
Page_Load
handler? And the lineIs 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.