确定应用程序在换班期间是否处于活动状态

时间:2018-04-22 05:58:46

标签: vb.net dictionary session timespan

VB2012:我的应用程序需要用户登录。一旦登录应用程序最小化到通知区域,因此有时用户忘记在工作班次结束后退出。我正在尝试检测工作班次何时更改以及应用程序是否仍处于活动状态,然后通知当前用户他们应该退出应用程序并重新登录。

到目前为止,我在计时器上确定了最后一次工作班次的开始时间:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'assemble the shifts start date/times
    Dim currDateTime As DateTime = DateTime.Now                     'current date and time
    Dim currDate As DateTime = currDateTime.Date                    'current date only
    Dim mdPriDate As DateTime = currDate.AddDays(-1).AddHours(22)   'prior day midnight shift starts at 22:00
    Dim amCurDate As DateTime = currDate.AddHours(6)                'current day morning shift starts at 06:00
    Dim pmCurDate As DateTime = currDate.AddHours(14)               'current day evening shift starts at 14:00
    Dim mdCurDate As DateTime = currDate.AddHours(22)               'current day midnight shift starts at 22:00

    'create a dictionary comprised of the shift start times and TimeSpans from current time
    Dim dictShifts As New Dictionary(Of DateTime, TimeSpan) From
        {
            {mdPriDate, currDateTime.Subtract(mdPriDate)}, _
            {amCurDate, currDateTime.Subtract(amCurDate)}, _
            {pmCurDate, currDateTime.Subtract(pmCurDate)}, _
            {mdCurDate, currDateTime.Subtract(mdCurDate)}
        }

    'output variables
    Debug.Print("currDate    ={0}", currDate.ToString("s"))
    Debug.Print("currDateTime={0}", currDateTime.ToString("s"))
    Debug.Print("mdPriDate   ={0} hoursElapsed={1}", mdPriDate.ToString("s"), currDateTime.Subtract(mdPriDate).TotalHours)
    Debug.Print("amCurDate   ={0} hoursElapsed={1}", amCurDate.ToString("s"), currDateTime.Subtract(amCurDate).TotalHours)
    Debug.Print("pmCurDate   ={0} hoursElapsed={1}", pmCurDate.ToString("s"), currDateTime.Subtract(pmCurDate).TotalHours)
    Debug.Print("mdCurDate   ={0} hoursElapsed={1}", mdCurDate.ToString("s"), currDateTime.Subtract(mdCurDate).TotalHours)

    'get only the timespans that are in the past and then order ascending by timespan. the first item should be the most current
    'shift start time.
    Dim vpLastShift As KeyValuePair(Of DateTime, TimeSpan) = dictShifts.Where(Function(x, y) x.Value.TotalHours >= 0).OrderBy(Function(z) z.Value).First()
    Debug.Print("last shift date={0} hoursElapsed={1}", vpLastShift.Key.ToString("s"), vpLastShift.Value.TotalHours)
End Sub

我基本上是在计算最后一次工作班次开始时。以下是调试示例:

currDate    =2018-04-22T00:00:00
currDateTime=2018-04-22T00:40:15
mdPriDate   =2018-04-21T22:00:00 hoursElapsed=2.670896461
amCurDate   =2018-04-22T06:00:00 hoursElapsed=-5.329103539
pmCurDate   =2018-04-22T14:00:00 hoursElapsed=-13.329103539
mdCurDate   =2018-04-22T22:00:00 hoursElapsed=-21.329103539
last shift date=2018-04-21T22:00:00 hoursElapsed=2.670896461

问题:计算最后一个工作班次开始时间,这种逻辑是否有效且合理?如何向用户发送通知?我想如果应用程序在最后一次换班前15分钟有效并且在换班后15分钟也有效,那么我应该通知用户退出。寻找关于我如何管理它的建议。

0 个答案:

没有答案