Asp.net mvc 在预定事件发生72小时前唤醒并发送电子邮件警报的并行过程
我有一个用ASP.NET MVC 2编写的应用程序,它在MSSQL数据库中存储用户计划事件的日期和时间。我需要应用程序在存储事件发生前72小时向用户发送电子邮件警报,无需任何手动干预Asp.net mvc 在预定事件发生72小时前唤醒并发送电子邮件警报的并行过程,asp.net-mvc,multithreading,scheduled-tasks,Asp.net Mvc,Multithreading,Scheduled Tasks,我有一个用ASP.NET MVC 2编写的应用程序,它在MSSQL数据库中存储用户计划事件的日期和时间。我需要应用程序在存储事件发生前72小时向用户发送电子邮件警报,无需任何手动干预 实现此并行过程的最佳方法是什么(我已经有了电子邮件代码)?假设您将事件存储在数据库中,我将创建一个独立于asp.net应用程序运行的Windows服务,它定期轮询数据库,寻找要完成的工作。如果您可以控制服务器,那么我可能会选择Windows服务(如) 但假设您不这样做,我将创建一个类,可能类似于下面的一个,您可以使
实现此并行过程的最佳方法是什么(我已经有了电子邮件代码)?假设您将事件存储在数据库中,我将创建一个独立于asp.net应用程序运行的Windows服务,它定期轮询数据库,寻找要完成的工作。如果您可以控制服务器,那么我可能会选择Windows服务(如) 但假设您不这样做,我将创建一个类,可能类似于下面的一个,您可以使用/继承它。在global.asax中的“Application_start”事件期间实例化此类,并将其副本保存在缓存中。将逻辑放在“ExecuteProcess”方法中。您的逻辑可能类似于(伪代码): 确保MSSQL数据库表中有一个字段用于跟踪是否已发送通知 基本基类:
Imports System.Threading
Public MustInherit Class LongRunningProcess
Public ReadOnly Property Running() As Boolean
Get
Return _Running
End Get
End Property
Public ReadOnly Property Success() As Boolean
Get
Return _Success
End Get
End Property
Public ReadOnly Property Exception() As Exception
Get
Return _Exception
End Get
End Property
Public ReadOnly Property StartTime() As DateTime
Get
Return _StartTime
End Get
End Property
Public ReadOnly Property EndTime() As DateTime
Get
Return _EndTime
End Get
End Property
Public ReadOnly Property Args() As Object
Get
Return _Args
End Get
End Property
Protected _Running As Boolean = False
Protected _Success As Boolean = False
Protected _Exception As Exception = Nothing
Protected _StartTime As DateTime = DateTime.MinValue
Protected _EndTime As DateTime = DateTime.MinValue
Protected _Args() As Object = Nothing
Protected WithEvents _Thread As Thread
Private _locker As New Object()
Public Sub Execute(ByVal Arguments As Object)
SyncLock (_locker)
'if the process is not running, then...'
If Not _Running Then
'Set running to true'
_Running = True
'Set start time to now'
_StartTime = DateTime.Now
'set arguments'
_Args = Arguments
'Prepare to process in a new thread'
_Thread = New Thread(New ThreadStart(AddressOf ExecuteProcess))
'Start the thread'
_Thread.Start()
End If
End SyncLock
End Sub
Protected MustOverride Sub ExecuteProcess()
End Class
Args属性允许您从“ExecuteProcess”中访问可能需要的任何参数/变量。此过程的并行性是什么?电子邮件警报将与其他应用程序功能并行发送。这是一种非正统的用法。:)我无法控制服务器,因此我将用C#重新编写您的解决方案,并尝试一下-谢谢!
Imports System.Threading
Public MustInherit Class LongRunningProcess
Public ReadOnly Property Running() As Boolean
Get
Return _Running
End Get
End Property
Public ReadOnly Property Success() As Boolean
Get
Return _Success
End Get
End Property
Public ReadOnly Property Exception() As Exception
Get
Return _Exception
End Get
End Property
Public ReadOnly Property StartTime() As DateTime
Get
Return _StartTime
End Get
End Property
Public ReadOnly Property EndTime() As DateTime
Get
Return _EndTime
End Get
End Property
Public ReadOnly Property Args() As Object
Get
Return _Args
End Get
End Property
Protected _Running As Boolean = False
Protected _Success As Boolean = False
Protected _Exception As Exception = Nothing
Protected _StartTime As DateTime = DateTime.MinValue
Protected _EndTime As DateTime = DateTime.MinValue
Protected _Args() As Object = Nothing
Protected WithEvents _Thread As Thread
Private _locker As New Object()
Public Sub Execute(ByVal Arguments As Object)
SyncLock (_locker)
'if the process is not running, then...'
If Not _Running Then
'Set running to true'
_Running = True
'Set start time to now'
_StartTime = DateTime.Now
'set arguments'
_Args = Arguments
'Prepare to process in a new thread'
_Thread = New Thread(New ThreadStart(AddressOf ExecuteProcess))
'Start the thread'
_Thread.Start()
End If
End SyncLock
End Sub
Protected MustOverride Sub ExecuteProcess()
End Class