所以我有 UI 可以在我的应用程序中创建一个日历事件。创建新事件时,我会为我的所有用户(大约 3,000 个)创建通知。我新这需要一段时间,因为我需要为每个用户写入数据库来创建他们的通知,所以我创建了一个继承自BackgroundWorker
. 我真的不在乎是否创建了通知(我这样做,但不是在为最终用户完成请求的上下文中)所以我认为这将是一种有效的方法。
然而,当我去实现它时,即使在调用之后context.Response.End()
,HttpHandler
仍然等待后台工作人员完成。我调试了线程并且HttpHandler
有BackgroundWorker
不同的线程ID。我不确定我是否在某种程度上抹黑了HttpHandler
返回,或者我是否误解了这BackgroundWorker
门课的用途。
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
...
}
}
}
任何见解都会很棒。提前致谢!