问题在Asp.net不同的线程访问文件 [英] Problem accessing file from different thread in Asp.net

查看:98
本文介绍了问题在Asp.net不同的线程访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网站的过程(使用Asp.net 3.5 LINQ到SQL进行数据存取),需要的工作方式如下:


  1. 上传文件

  2. 记录并保存有关文件的数据库信息

  3. 从文件导入数据到数据库

  4. 重定向到不同的页面

在这样的顺序运行,一切正常。但是,由于要导入的文件可能非常大,我想第3步,从UI线程不同的线程上运行。用户应该到第4步,而第3步仍在进行中,并在第4步屏幕会定期更新,让用户知道导入完成后。

我处理的线程如下:

 公共类导入{  公共静态无效ImportPendingFile(){
    导入I =新导入();
    螺纹newThread =新主题(新的ThreadStart(i.ImportFile));
    newThread.Start();
  }  公共无效的importfile(){
    // 1.查询数据库,以确定挂起的文件
    //起来2.打开和解析挂起的文件
    // 3.从文件导入所有数据到数据库
    // 4.更新数据库,以反映导入成功完成
  }
}

和在codebehind:

 保护无效butUpload(对象发件人,EventArgs的发送){
  //保存文件,导入prepare
  Import.ImportPendingFile();
  的Response.Redirect(NewLocation);
}

在这样做,我能够通过新的线程正常启动调试器进行确认。但是,每当我这样做,尝试访问该文件(步骤2中的code后面)当线程中止。在主线程中运行时,所以一些关于多线程的情况是preventing这这工作得很好。我原以为,既然文件保存到磁盘(它是)有不应该用在不同的线程中打开它的任何问题。任何想法,我已经错了,我该如何解决?谢谢!

请注意:我使用的是第三方组件,打开该文件。使用反射,我发现与此相关的是如何打开的文件中的以下code:

 如果(File.Exists(文件名)){
  使用(的FileStream流=新的FileStream(文件名,FileMode.Open)){
    //使用流来打开文件
  }
}


解决方案

尝试的Response.Redirect(网址,假),否则'回应'将只是调用之后结束。

I have a process in a website (Asp.net 3.5 using Linq-to-Sql for data access) that needs to work as follows:

  1. Upload file
  2. Record and save info regarding file to database
  3. Import data from file into database
  4. Redirect to different page

When run sequentially like this, everything works fine. However, since the files being imported can be quite large, I would like step 3 to run on a different thread from the UI thread. The user should get to step 4 while step 3 is still in progress, and the screen on step 4 will periodically update to let the user know when the import is complete.

I am handling the threading as follows:

public class Import {

  public static void ImportPendingFile() {
    Import i = new Import();
    Thread newThread = new Thread(new ThreadStart(i.ImportFile));
    newThread.Start();
  }

  public void ImportFile() {
    // 1. Query DB to identify pending file
    // 2. Open up and parse pending file
    // 3. Import all data from file into DB
    // 4. Update db to reflect that import completed successfully
  }
}

And in the codebehind:

protected void butUpload(object sender, EventArgs e) {
  // Save file, prepare for import
  Import.ImportPendingFile();
  Response.Redirect(NewLocation);
}

When doing this, I am able to confirm via debugger that the new thread is starting up properly. However, whenever I do this, the thread aborts when trying to access the file (step 2 in the code behind). This works fine when run in the main thread, so something about the multi-threaded situation is preventing this. I had thought that since the file is saved to disk (which it is) that there shouldn't be any problem with opening it up in a different thread. Any ideas where I have gone wrong and how I can fix it? Thanks!

Note: I am using a third-party assembly to open the file. Using reflector, I have found the following code related to how it opens up the file:

if (File.Exists(fileName)) {
  using (FileStream stream = new FileStream(fileName, FileMode.Open)) {
    // use stream to open file
  }
}

解决方案

Try Response.Redirect(url, false) , else the 'Response' will be ended just after that call.

这篇关于问题在Asp.net不同的线程访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆