从Asp.net中的不同线程访问文件时出现问题

时间:2022-10-06 21:02:05

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

我在一个网站(使用Linq-to-Sql进行数据访问的Asp.net 3.5)中有一个进程需要按如下方式工作:

  1. Upload file
  2. Record and save info regarding file to database
  3. 记录文件并将其保存到数据库

  4. Import data from file into database
  5. 将数据从文件导入数据库

  6. Redirect to different page
  7. 重定向到不同的页面

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.

像这样按顺序运行时,一切正常。但是,由于导入的文件可能非常大,我希望步骤3在UI线程的不同线程上运行。当步骤3仍在进行时,用户应该进入步骤4,并且步骤4中的屏幕将定期更新以让用户知道导入何时完成。

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!

执行此操作时,我可以通过调试器确认新线程正在正常启动。但是,每当我这样做时,线程在尝试访问文件时中止(后面的代码中的步骤2)。这在主线程中运行时工作正常,因此关于多线程情况的一些事情阻止了这一点。我曾经想过,因为文件保存到磁盘(它是),在不同的线程中打开它应该没有任何问题。我出错的任何想法以及我如何解决它?谢谢!

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

2 个解决方案

#1


1  

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

尝试Response.Redirect(url,false),否则'Response'将在该调用之后结束。

#2


0  

ASP.NET view state is a great feature and an essential tool for web development of today. It maintains the state of a page as it travels back and forth. There is no more need to worry about restoring values of page controls between postbacks. In this article you will get an in-depth perspective on view state. We will talk about ways of reducing unnecessary payload and protecting view state from prying eyes.

ASP.NET视图状态是一个很棒的功能,是当今Web开发的必备工具。它在来回传播时保持页面的状态。不再需要担心在回发之间恢复页面控件的值。在本文中,您将深入了解视图状态。我们将讨论减少不必要的有效负载和保护视图状态免受窥探的方法。

#1


1  

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

尝试Response.Redirect(url,false),否则'Response'将在该调用之后结束。

#2


0  

ASP.NET view state is a great feature and an essential tool for web development of today. It maintains the state of a page as it travels back and forth. There is no more need to worry about restoring values of page controls between postbacks. In this article you will get an in-depth perspective on view state. We will talk about ways of reducing unnecessary payload and protecting view state from prying eyes.

ASP.NET视图状态是一个很棒的功能,是当今Web开发的必备工具。它在来回传播时保持页面的状态。不再需要担心在回发之间恢复页面控件的值。在本文中,您将深入了解视图状态。我们将讨论减少不必要的有效负载和保护视图状态免受窥探的方法。