首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BackgroundWorker仍会阻止IHttpHandler

BackgroundWorker仍会阻止IHttpHandler
EN

Stack Overflow用户
提问于 2012-09-16 22:22:26
回答 2查看 304关注 0票数 4

因此,我有UI来创建我的应用程序中的日历事件。当创建一个新事件时,我会为我的所有用户(大约3000个)创建通知。我知道这需要一段时间,因为我需要为每个用户写入数据库来创建他们的通知,所以我创建了一个继承自BackgroundWorker的类。我真的不关心是否创建了通知(我是这样做的,但不是在为最终用户完成请求的上下文中),所以我认为这是一种有效的方法。

然而,当我去实现它时,即使在调用了context.Response.End()之后,HttpHandler仍然等待后台工作者完成。我调试了线程,HttpHandlerBackgroundWorker具有不同的线程ids。我不确定我是以某种方式阻止了HttpHandler的返回,还是误解了BackgroundWorker类的作用。

代码语言:javascript
复制
class EventHandler : IHttpHandler
{
    ...
    public void ProcessRequest(HttpContext context)
    {
         ...
         // I need this to finish before the response ends
         CalendarEvent event = CreateCalendarEvent();
         List<int> users = GetUsersFromDB();
         if(event != null) // The event was created successfully so create the notifications
         {
             // This may take a while and does not effect the UI on
             // client side, so it can run in the background
             NotificationBackgroundWorker notificationWorker = new NotificationBackgroundWorker(notification, users);
             notificationWorker.RunWorkerAsync();
         } else {
            ...
            // Log Error and set status code for response
            ...
         }
         ...
         context.Response.End()
    }
    ...
}

class NotificationBackgroundWorker : BackgroundWorker
{
    private Notification notification;
    private List<int> users;

    public NotificationBackgroundWorker(Notification newNotification, List<int> usersToNotify) : base()
    {
        this.notification = newNotification;
        this.users = usersToNotify;
        this.DoWork += DoNotificationWork;
    }

    private void DoNotificationWork(object sender, DoWorkEventArgs args)
    {
        CreateUserNotifications(notification, users);
    }

    private void CreateUserNotifications(Notification notification, List<int> userList)
    {
        // This is where the bottleneck is occurring because there 
        // is one DB write per user
        foreach (int userId in userList)
        {
            ...
            // Create the notification for each user
            ...
        }
     }
}

任何洞察力都会很棒。提前感谢大家!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-17 03:14:19

BackgroundWorker与当前的SynchronizationContext集成。它的设计方式是在BackgroundWorker完成之前不让当前请求结束。这是你大多数时候想要的。

我建议您通过启动一个新的Task将工作项排队到线程池中。

票数 5
EN

Stack Overflow用户

发布于 2012-09-17 02:42:58

在另一个线程中完成,而不是使用BackgroundWorker。我不确定BackGroudWorker是否会在这种情况下工作。

代码语言:javascript
复制
public void ProcessRequest(HttpContext context)
    {
         ...
       // I need this to finish before the response ends
       CalendarEvent event = CreateCalendarEvent();
       List<int> users = GetUsersFromDB();
       if(event != null) // The event was created successfully so create the notifications
       {

             Thread thread = new Thread(
            () =>
             {
                CreateUserNotifications(notification, users);
             });
             thread .IsBackground = true;
            thread.Start();

       } else {
            ...
            // Log Error and set status code for response
            ...
         }
         ...
         context.Response.End()
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12447636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档