简体   繁体   English

ThreadPool.QueueUserWorkItem完成事件?

[英]ThreadPool.QueueUserWorkItem completed event?

When a user clicks a button, I use the ThreadPool.QueueUserWorkItem to spawn a thread that kicks off a long running process. 当用户单击一个按钮时,我使用ThreadPool.QueueUserWorkItem产生一个启动长时间运行的线程的线程。 I would like to make a button visible when the thread is completed so that user can click on it. 我想在线程完成后使按钮可见,以便用户可以单击它。

Is there a completed event in ThreadPool ThreadPool是否有完成的事件

What am I trying to do? 我想做什么?

One of the pages has a button when clicked connects to a WS and kicks off a long running process that creates a file. 其中一个页面上有一个按钮,单击该按钮将连接到WS,并启动一个长时间运行的过程来创建文件。 During the processing time, I would like to show a progress bar to the user. 在处理期间,我想向用户显示进度条。 Once the processing is completed, I would like to provide a link to the file that was created. 处理完成后,我想提供一个指向所创建文件的链接。 Many users will be clicking on the button the same time that creates different files. 许多用户将同时单击按钮来创建不同的文件。 I guess you would call this a single request that can be implemented using the Async approach? 我想您将其称为可以使用异步方法实现的单个请求?

Quick Qs about Async pages: 关于异步页面的快速问答:

  1. The MSDN link has the following code: MSDN链接具有以下代码:

    AddOnPreRenderCompleteAsync ( new BeginEventHandler(MyBeginMethod), new EndEventHandler (MyEndMethod) ); AddOnPreRenderCompleteAsync(new BeginEventHandler(MyBeginMethod),new EndEventHandler(MyEndMethod));

It appears that the MyBeginMethod is kicked off on Page Load, will the same code snippet work for me if i want to initiate the async process on a button click? 看来MyBeginMethod在Page Load上启动了,如果我想在单击按钮时启动异步过程,相同的代码段对我MyBeginMethod吗?

  1. Can I access the UI controls in MyBeginMethod and MyEndMethod methods (so that I can make a download file button visible on the process completion? Or does a different thread own these controls? 我可以访问MyBeginMethodMyEndMethod方法中的UI控件(以便使下载文件按钮在过程完成时可见)还是其他线程拥有这些控件?

Nope. 不。

You'll have to store the fact you've completed somewhere available to your ASP.NET pages and the method doing your work, like the HttpCache. 您必须将已完成的事实存储在ASP.NET页可用的位置以及执行工作的方法(例如HttpCache)中。

If this is all happening within a single request, skip the thread pool and check out asynchronous pages. 如果这一切都在单个请求中发生,请跳过线程池并检出异步页面。


First, if the file is created per user, then the result of the long running action should be stored in the Session, not the cache. 首先,如果文件是按用户创建的,则长时间运行操作的结果应存储在会话中,而不是缓存中。 The cache is shared between users, and the session is per-user. 缓存在用户之间共享,会话是每个用户的。

The long running task can create the file, then store the file path in the session for that user. 长时间运行的任务可以创建文件,然后在该用户的会话中存储文件路径。 Whenever the user sends another request for the file, the session is checked for this file. 每当用户发送对文件的另一个请求时,都会在会话中检查该文件。

There are a few options for the UI during this. 在此期间,UI有一些选项。 One is by postbacks to the server, the other is ajax calls. 一种是通过回发到服务器,另一种是通过ajax调用。

Ajax may not be the easiest, even though there are plenty of frameworks (like jQuery) and lots of information on the net about how to make an asynchronous postback. 即使有很多框架(如jQuery)并且网上有很多有关如何进行异步回发的信息,Ajax可能也不是最简单的。 If the process is very long, users might browse away, etc, making your task harder to implement. 如果过程很长,用户可能会浏览等等,这使您的任务难以实施。

Another option is to make your request for the file, then redirect to a holding page. 另一种选择是向文件提出请求,然后重定向到保留页面。 This holding page has a script that automatically refreshes every X seconds. 该保留页面具有一个脚本,该脚本每X秒自动刷新一次。 It displays to the user a notification such as "Please wait, creating your file." 它向用户显示诸如“请稍候,正在创建文件”之类的通知。 Every time this page posts back to the server the existence of the file is checked. 每次此页面发回服务器时,都会检查文件的存在。 If its not ready yet, then you return to the holding page. 如果尚未准备好,则返回到保留页面。 Eventually, the file becomes ready and you can redirect the user to another page where the file is downloaded/displayed/whatever. 最终,文件准备就绪,您可以将用户重定向到另一个页面,在该页面上可以下载/显示/进行任何操作。

The second option is probably easier and more straightforward for you to implement. 第二种选择可能更容易实现,也更直接。 Here's the pseudocode: 这是伪代码:

  1. User clicks a link to get the file 用户单击链接以获取文件
  2. On postback, server redirects to the holding page 在回发时,服务器重定向到保留页面
  3. User's browser is redirected to the holding page 用户的浏览器被重定向到保留页面
  4. Request is handled by the holding page. 请求由保留页面处理。 The Session is checked for the file. 检查会话中的文件。
    1. If the session reports the file has not been, and is not being created, the file creation process is started. 如果会话报告文件尚未创建,并且尚未创建,那么将启动文件创建过程。 The holding page is returned to the user. 保留页面返回给用户。
    2. If the session reports the file is being created, the holding page is returned to the user. 如果会话报告正在创建文件,则将保留页面返回给用户。
    3. If the session reports the file is made, the user is redirected to the download page 如果会话报告已完成文件,则将用户重定向到下载页面
  5. The holding page has a javascript method that runs on load that reloads the page every X seconds. 保留页面具有一个可在加载时运行的javascript方法,该方法每X秒重新加载该页面。

No, there is no completed event in ThreadPool , nor is there an easy way to Join() . 不,在ThreadPool没有完成的事件,也没有一种简单的Join()

That is one of the issues that the Task class solves in .NET4. 这是Task类在.NET4中解决的问题之一。 Tasks have a Wait() method. 任务具有Wait()方法。

In the mean time, follow the recommendations from Will's answer. 同时,请遵循Will的答案中的建议。 Asynchronous ASP.NET is easy to get wrong. 异步ASP.NET容易出错。

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

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