Sign in to follow this  
Followers 0
User0101

Windows Shutdown Query Issue

6 posts in this topic

Situation:

I have a VPN application that runs for a period of 1 hour after a user has finished their work before being scripted to terminate. However, if a user initiates a Windows shutdown through the Start Menu this VPN application will display an error message about being closed incorrectly. Even though no harm is done to the VPN software, and the computer does shutdown as the user has initiated without any intervention, I need to remove a user's ability to see this error message by terminating the VPN application and then sending a command to shutdown the PC. If the user does not initiate a Windows shutdown then I still need the 1 hour pause to be in effect. A loop statement is the only thing I can think of that accomplishes something like this.

Here is the work I have so far, taken largely in part from the information gathered here in the forums and research. At this point I can not seem to grasp how to make this happen. Thank you for your time and help on this issue. After a full day of research, checking help files, testing, and browsing similar scripting scenarios I've decided the best course of action is to ask for help.

$WM_QUERYENDSESSION = 0x0011
GUIRegisterMsg($WM_QUERYENDSESSION, "Cancel_Shutdown")
GUICreate("PreventShutdownGUI")
GUISetSTate(@SW_HIDE)
Global $b_ShutdownInitiated = False

Func Cancel_Shutdown($hWndGUI, $MsgID, $WParam, $LParam)
    $b_ShutdownInitiated = True
    Return False
EndFunc

While
    If $b_ShutdownInitiated = False Then
        Sleep(3600000)
    ElseIf $b_ShutdownInitiated = True Then
        ProcessClose("nsload.exe")
        Sleep (5000)
        _RunDOS("SHUTDOWN.exe -s -t 00")
        Exit
    EndIf
    sleep(10)
WEnd

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

While
    If $b_ShutdownInitiated = False Then
        Sleep(3600000)
    ElseIf $b_ShutdownInitiated = True Then
        ProcessClose("nsload.exe")
        Sleep (5000)
        _RunDOS("SHUTDOWN.exe -s -t 00")
        Exit
    EndIf
    sleep(10)
WEnd

The problem with this is that when the While loop starts, it goes into the long Sleep statement which basically just makes the script idle and prevents further checks on the state of $b_ShutdownInitiated.

One way to handle this situation is to use a Timer and regularly check whether the script has already slept for 1 hour.

TimerInit()
While 1
    If TimerDiff() >= 3600000 Then
        Exit ;Or whatever else you want to happen after a 1 hour sleep
    ElseIf $b_ShutdownInitiated = True Then
        ProcessClose("nsload.exe")
        Sleep(5000)
        Run("SHUTDOWN.exe -s -t 0")
        Exit
    EndIf
    Sleep(10)
WEnd

Just check if I did this correctly, since I don't normally use Timer functions in my scripts.

Edited by omikron48

Share this post


Link to post
Share on other sites

Interesting and great idea. Let me continue to check.

I wish my scenario was a bit different but I need the sleep function to happen while simultaneously detecting if a shutdown command is sent during that process. Bit of a cat and mouse game and not sure if I can pull it off.

Share this post


Link to post
Share on other sites

While tested syntax I received this error.

ERROR: TimerDiff() [built-in] called with wrong number of args.

If TimerDiff()

~~~~~~~~~~~~~~~~~^

Share this post


Link to post
Share on other sites

While tested syntax I received this error.

ERROR: TimerDiff() [built-in] called with wrong number of args.

If TimerDiff()

~~~~~~~~~~~~~~~~~^

Hi,

$start = TimerInit ()

..

If TimerDiff ($start)

Share this post


Link to post
Share on other sites

Ahh... I see where I went wrong.

Thank you both. Syntax is good so now I'm off to testing. I'll update the post soon. Again, thank you both.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0