Jump to content

disable exit during a critical part of the script


Recommended Posts

I have a script that does safe things most of time.  When particular situations are detected, it sends an email and log the action ("email xyz sent - timestamp") into a file.

Some times I want to manually Exit the script (for making corrections or just pausing it) by clicking Exit on the tray icon of the script, or by ctrl+break if launched inside SciTE.

In this, there is a minimal risk that I exit the script in the middle between the send email and the log actions, although there're immediately consecutive.

What's the most proper way to avoid this risk?

I'd like something like this:
 

- code...
- code...
- code...
- #disable Exit   <-  how to do this?
- send email
- log email sent
- #enable Exit   <-  how to do this?
- code...
- code...

 

Link to comment
Share on other sites

You could use BlockInput but you must have #RequireAdmin with the annoying UAC that comes along.

If you want a pseudo-disable, you could define all the "risk" keys (like alt-f4) with HotKeySet () and eat those keys. 

For the mouse you could use the _MouseTrap () trick like this :

$pos = MouseGetPos ()
_MouseTrap ($pos[0],$pos[1])

 

Link to comment
Share on other sites

Hi Imbuter2000 :)
Just tried this, it seems to do the job. The following script will hide the tray icon after 5 seconds (and during 5 seconds), then it will reappear. During these 5 "eclipse" seconds, Ctrl-Break won't stop the script.

AdlibRegister("MyAdLibFunc", 1000) ; each second
Sleep(15000) ; 15s
AdlibUnRegister("MyAdLibFunc")
MsgBox(0, "", "Finished")

Func MyAdLibFunc()
    Local Static $iCount = 0
    $iCount += 1
    ConsoleWrite("MyAdLibFunc called " & $iCount & " time" & _
        (($iCount > 1) ? ("s") : ("")) & @CRLF)

    Switch $iCount
        Case 5
            ConsoleWrite("Hiding Tray Icon, Disabling Control+Break during 5 seconds" & @CRLF)
            Opt("TrayIconHide", 1) ;0=show, 1=hide tray icon
            HotKeySet("^{BREAK}", "Unbreakable")
        Case 10
            ConsoleWrite("Showing Tray Icon, Enabling Control+Break" & @CRLF)
            Opt("TrayIconHide", 0) ;0=show, 1=hide tray icon
            HotKeySet("^{BREAK}")
    EndSwitch
EndFunc   ;==>MyAdLibFunc

Func Unbreakable()
    ; MsgBox(0, "", "Ctrl+Break pressed")
Return   ;==>Unbreakable

You could use both instructions Opt("TrayIconHide") and HotKeySet("^{BREAK}") placed in your script, at the moment you don't want your script to be exited, then reuse them a bit later to go back to normal, using appropriate syntax.

Edited by pixelsearch
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...