首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >并行处理,可在预定事件前72小时唤醒和发送电子邮件警报

并行处理,可在预定事件前72小时唤醒和发送电子邮件警报
EN

Stack Overflow用户
提问于 2011-06-22 02:30:03
回答 2查看 283关注 0票数 0

我有一个用MSSQLMVC2编写的应用程序,它将用户预定事件的日期和时间存储在ASP.NET数据库中。我需要应用程序发送一个电子邮件警报到用户72小时前存储的事件发生,没有任何人工干预。

实现这种并行过程的最佳方式是什么(我已经有了电子邮件代码)?

EN

回答 2

Stack Overflow用户

发布于 2011-06-22 02:33:00

假设您已将事件存储在数据库中,我将创建一个Windows Service,它独立于asp.net应用程序运行,该应用程序定期轮询数据库以查找要完成的工作。

票数 0
EN

Stack Overflow用户

发布于 2011-06-22 02:40:58

如果你可以控制服务器,那么我可能会选择Windows Service (如The Evil Greebo said)。

但假设您没有这样做,我将创建一个类,可能类似于下面的类,您可以使用/继承它。在global.asax中的"Application_start“事件期间实例化这个类,并在缓存中保存它的副本。将您的逻辑放在"ExecuteProcess“方法中。你的逻辑可能看起来像(伪代码):

代码语言:javascript
复制
while(true) 
   check for events that need to have notifications sent

   send any needed notifications

   wait x seconds and repeate

loop

请确保您的MSSQL数据库表中有一个字段,用于跟踪是否已发送通知。

基本基类:

代码语言:javascript
复制
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“中访问您可能需要的任何参数/变量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6430203

复制
相关文章

相似问题

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