定时提醒器框架

ActionBase.vb

Public MustInherit Class ActionBase

    Public Property Name As String

    Public MustOverride Function Start() As Boolean

End Class

 

AlertAction.vb

Public Class AlertAction
    Inherits ActionBase

    Public Property MessageTitle As String = String.Empty

    Public Property MessageText As String = String.Empty

    Public Overrides Function Start() As Boolean
        MsgBox(Me.MessageText, MsgBoxStyle.OkOnly, Me.MessageTitle)
        Return True
    End Function
End Class

 

ScheduleBase.vb

Public MustInherit Class ScheduleBase

    Public Property Name As String

    Public MustOverride Function GetNextTime() As Nullable(Of DateTime)

    Public MustOverride Sub Execute()

    Public Property BeginTime As Date = DateTime.Today

    Public Property EndTime As Date = DateTime.Today

End Class


WeeklySchedule.vb

Imports System.Xml.Schema

Public Class WeeklySchedule
    Inherits ScheduleBase

    Private weekList(7) As Boolean

    Public Overrides Function GetNextTime() As Nullable(Of DateTime)
        For i As Integer = 0 To 7
            Dim destWeekDay As DateTime = DateTime.Now.AddDays(i)
            If Not weekList(destWeekDay.DayOfWeek) Then
                Continue For
            End If
            If destWeekDay.Date = DateTime.Today Then
                If DateTime.Now.TimeOfDay > ExecuteTime Then
                    Continue For
                End If
            End If
            Return destWeekDay.Date.Add(ExecuteTime)
        Next

        Return Nothing
    End Function

    Public Overrides Sub Execute()
        If Not Me.Action Is Nothing Then
            Me.Action.Start()
        End If
    End Sub

    REM "Extension"

    Public Property ExecuteTime As TimeSpan

    Public Function GetWeekDayState(ByVal dayIndex As Integer) As Boolean
        Return weekList(dayIndex)
    End Function

    Public Sub SetWeekDayState(ByVal dayIndex As Integer, ByVal state As Boolean)
        weekList(dayIndex) = state
    End Sub

    Public Property Action As ActionBase

End Class

 

frmMain.vb

Public Class frmMain

    Private Delegate Sub AsyncExecute()

    Private JobList As New List(Of ScheduleBase)

    Private Sub AddAWeeklyMeeting()
        Dim job As New WeeklySchedule
        job.Name = "QBE Weekly Meeting"
        job.ExecuteTime = New TimeSpan(13, 45, 0)
        Dim action = New AlertAction()
        action.MessageTitle = job.Name
        action.MessageText = "开会了"
        job.Action = action
        job.SetWeekDayState(3, True) '周三
        job.SetWeekDayState(4, True) '周四
        job.SetWeekDayState(5, True) '周五
        JobList.Add(job)
    End Sub

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddAWeeklyMeeting()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        For Each job As ScheduleBase In JobList
            If job.BeginTime > DateTime.Now.Date Or job.EndTime < DateTime.Now.Date Then
                Continue For
            End If

            Dim nextRunTime As Nullable(Of DateTime) = job.GetNextTime
            If nextRunTime Is Nothing Then
                Continue For
            End If

            Debug.Print(nextRunTime.ToString())

            Dim nextTime As DateTime = nextRunTime.Value
            Dim currentTime As DateTime = DateTime.Now

            nextTime = New DateTime(nextTime.Year, nextTime.Month, nextTime.Day, nextTime.Hour, nextTime.Minute, nextTime.Second)
            currentTime = New DateTime(currentTime.Year, currentTime.Month, currentTime.Day, currentTime.Hour, currentTime.Minute, currentTime.Second)
            If nextTime <> currentTime Then
                Continue For
            End If

            Dim asyncMethod As AsyncExecute = New AsyncExecute(AddressOf job.Execute)
            asyncMethod.BeginInvoke(Nothing, Nothing)

        Next
    End Sub
End Class

 

 

posted on 2014-07-15 10:09  空明流光  阅读(281)  评论(0编辑  收藏  举报

导航