长时间运行的VBScript文件警报

时间:2015-03-13 10:02:56

标签: loops time vbscript do-while

我正在尝试构建一个VBScript文件来提醒教师(一旦他们已经登录)我在学校时需要注册(在这种情况下,每节课15分钟)。

我是通过尝试无限循环来做这件事,并检查时间,然后提醒用户。但是,当运行时,仅当执行时间等于9:00或10:00等时才满足条件。我希望它在后台有效运行,并在时间等于9:00或10时发出警报:00等。

代码如下:

Do While True
    If Now() = #09:00# Or Now() >= #10:00# Or Now() >= #11:20# Or Now() >= #12:20# Or Now() >= #14:05# Then
        msg = Msgbox ("Have you done your register?",4+48+4096,"")
        If msg = 7 Then
            Msgbox "Please take it immediately.",48+4096,""
            Exit Do
        Else
            Exit Do
        End If
    End If
Loop

我尝试通过FormatDateTime()和具有相同结果的Do Until循环来获得不同的时间。

显然,如果我要摆脱Exit Do语句,它会在那一分钟内永远运行。我希望每次只提醒一次。

请帮忙!

2 个答案:

答案 0 :(得分:1)

有效!

我设法更改了脚本,以便读取正确的时间,并且脚本必须等待一分钟才能退出msgbox循环。然后,在最后一次阅读时,它显示最后一条消息并结束循环,但是,它仍将在后台运行,直到该人注销为止(我不认为这会给资源带来压力)。

Do While True
    ctime = FormatDateTime(Now(), 4)
    If ctime = "09:00" Or ctime = "10:00" Or ctime = "11:20" Or ctime = "12:20" Then
        msg = Msgbox ("Have you done your register?",4+32+4096,"")
        If msg = 7 Then
            Msgbox "Please take it immediately.",48+4096,""
            WScript.CreateObject("WScript.Shell").Run(path)
            WScript.Sleep 60000
        Else
            WScript.Sleep 60000
        End If
    ElseIf ctime = "14:05" Then
        msg = Msgbox ("Have you done your register?",4+32+4096,"")
        If msg = 7 Then
            Msgbox "Please take it immediately.",48+4096,""
            WScript.CreateObject("WScript.Shell").Run(path)
            WScript.Quit
            Exit Do
        Else
            WScript.Quit
            Exit Do
        End If
    End If
Loop

答案 1 :(得分:1)

你应该这样写:

Do While True
    ctime = FormatDateTime(Now(),4)
    If ctime = "09:00" Or ctime = "10:00" Or ctime = "11:20" Or ctime = "12:20" Then
        msg = Msgbox ("Have you done your register ?",4+32+4096,Now())
        If msg = VbNo Then
            Msgbox "Please take it immediately.",48+4096,Now()
            WScript.Sleep 60000
        Else
            WScript.Sleep 60000
        End If
    ElseIf ctime = "14:05" Then
        msg = Msgbox ("Have you done your register ?",4+32+4096,Now())
        If msg = VbNo Then
            Msgbox "Please take it immediately !",48+4096,Now()
            Exit Do
            WScript.Quit
        Else
            Exit Do
            WScript.Quit
        End If
    End If
Loop

我添加了一些有趣的东西,比如被网上的警报吓到了,如果你愿意,试试吧!

Option Explicit
If AppPrevInstance() Then 
    MsgBox "There is an existing proceeding !" & VbCrLF &_
    CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"    
    WScript.Quit   
Else 
    Dim ctime,msg,Time1,Time2,Time3,Time4,LastTime2GoAway,Alarm
    Time1 = "09:00" : Time2 = "10:00" : Time3 = "11:20" : Time4 = "12:20" : LastTime2GoAway = "14:05"
    Alarm = "http://soundbible.com/1080-Airhorn.html"
    Do While True
        ctime = FormatDateTime(Now(),4)
        If ctime = Time1 Or ctime = Time2 Or ctime = Time3 Or ctime = Time4 Then
            If CheckConnectionInternet() = True Then 
                Call Alert(Alarm)
                msg = Msgbox ("Have you done your register ?",4+32+4096,Now())
                If msg = VbNo Then
                    Msgbox "Please take it immediately.",48+4096,Now()
                    Call Pause(60)
                Else
                    Call Pause(60)
                End If
            Else    
                msg = Msgbox ("Have you done your register ?",4+32+4096,Now())
                If msg = VbNo Then
                    Msgbox "Please take it immediately.",48+4096,Now()
                    Call Pause(60)
                Else
                    Call Pause(60)
                End If
            End If
        ElseIf ctime = LastTime2GoAway Then
            If CheckConnectionInternet() = True Then 
                Call Alert(Alarm)
                msg = Msgbox ("Have you done your register ?",4+32+4096,Now())
                If msg = VbNo Then
                    Msgbox "Please take it immediately !",48+4096,Now()
                    Exit Do
                    WScript.Quit
                Else
                    Exit Do
                    WScript.Quit
                End If
            Else
                msg = Msgbox ("Have you done your register ?",4+32+4096,Now())
                If msg = VbNo Then
                    Msgbox "Please take it immediately !",48+4096,Now()
                    Exit Do
                    WScript.Quit
                Else
                    Exit Do
                    WScript.Quit
                End If
            End If
        End If
    Loop
End If
'**************************************************************************
Sub Alert(WebSite)
    Dim objExplorer
    Set objExplorer = CreateObject("InternetExplorer.Application")
    objExplorer.Navigate WebSite
    objExplorer.Visible = 0 'Invisible
    Do While (objExplorer.Busy)
        Wscript.Sleep 500
    Loop
    Wscript.Sleep 5000
    objExplorer.Quit
End Sub
'**************************************************************************
Function AppPrevInstance()   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
            " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
            AppPrevInstance = (.Count > 1)   
        End With   
    End With   
End Function    
'**************************************************************************
Function CommandLineLike(ProcessPath)   
    ProcessPath = Replace(ProcessPath, "\", "\\")   
    CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
'**************************************************************************
Function CheckConnectionInternet()
    Dim strComputer,objPing,objStatus
        strComputer = "smtp.gmail.com"
        Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}!\\").ExecQuery _
        ("select * from Win32_PingStatus where address = '" & strComputer & "'")
        For Each objStatus in objPing
            If objStatus.Statuscode = 0 Then
                CheckConnectionInternet = True
            Else
                CheckConnectionInternet = False
            End If
        Next
End Function
'**********************************************************************************************
Sub Pause(NSeconds)
    Wscript.Sleep(NSeconds*1000)
End Sub
'**************************************************************************************