简体   繁体   English

在 ASP.Net web 应用程序中存储进度信息的位置

[英]Where to store progress information in ASP.Net web application

I'm creating a page that get uploaded text files and builds them into multiple PDFs.我正在创建一个页面,用于获取上传的文本文件并将它们构建成多个 PDF。 They are just exports from Excel.它们只是 Excel 的出口。 Each row in the file corresponds to a new PDF that needs to be created.文件中的每一行对应一个需要新建的 PDF。

Anyway, once the files are uploaded I want to begin processing them, but I don't want the user to have to stay on the page, or even still have their session open.无论如何,一旦文件上传,我想开始处理它们,但我不希望用户必须留在页面上,甚至仍然打开他们的 session。 For example they could close the browser and come back 10 minutes later, log in, and the progress information will say like 112/200 files processed or something.例如,他们可以关闭浏览器并在 10 分钟后返回,登录,进度信息会显示 112/200 个文件已处理或其他内容。 It will be a lot quicker than that though.不过,它会比这快得多。

So two questions really, how can I pass this processing job to something (Handler?Thread?) that will continue to run when the page is closed, and will return as soon as the job has started (so the browser isn't stopped)?所以真的有两个问题,我怎样才能将此处理作业传递给在页面关闭时将继续运行的东西(处理程序?线程?),并在作业开始后立即返回(因此浏览器不会停止) ? Secondly, where can I store this information so that when the user comes back to the page, they can see the current progress.其次,我在哪里可以存储这些信息,以便当用户回到页面时,他们可以看到当前的进度。

I realise that I can't use sessions, and since it will be processing about a file a second I don't really want to update a DB every second.我意识到我不能使用会话,并且由于它将每秒处理一个文件,我真的不想每秒更新一个数据库。 Is there some way I can do this?有什么办法可以做到这一点吗? Is it possible?可能吗?

I solved this by using the link provided by astander above.我通过使用上面 asstander 提供的链接解决了这个问题。 I simply create an object in the HttpContext.Application to store progress variables, and then Set the method which does my processing inside a new Thread.我只是在 HttpContext.Application 中创建一个 object 来存储进度变量,然后设置在新线程中执行我的处理的方法。

// Create the new progress object
BatchProgress bs = new BatchProgress(0);
if(Application["BatchProgress"] != null)
{
    // Should never happen
    Application["BatchProgress"] = bs;
}
else
{
    Application.Add("BatchProgress","bs");
}

//Set up new thread, run batch is the method that does all the processing.
ThreadStart ts = new ThreadStart(RunBatch);
Thread t = new Thread(ts);
t.Start();

It then returns after the thread starts and I can use jQuery to get the Application["BatchProgress"] object at regular intervals.然后在线程启动后返回,我可以使用 jQuery 定期获取 Application["BatchProgress"] object 。 At the end of my thread the BatchProgress object has its status set to "Complete", then when jQuery queries it, it sees the complete status and removes the progress object from the application.在我的线程结束时,BatchProgress object 的状态设置为“完成”,然后当 jQuery 查询它时,它会看到完成状态并从应用程序中删除进度 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM