Jump to content

How to use hotkey to exit without blocking?!


Recommended Posts

Hello,

i have a script similar to this example:

HotKeySet("{ESC}", "Terminate")
While True
    ConsoleWrite("Running" & @CRLF)
    Sleep(500)
WEnd

Func Terminate()
    ConsoleWrite("Waiting" & @CRLF)
    $result = MsgBox(262148, "Exit?", "Are you sure you want to exit?")
    If $result = 6 Then
        Exit 0
    EndIf
EndFunc   ;==>Terminate

When i press esc it will ask me if i really want to exit.

As MsgBox is a blocking function, the whole script will pause until i answer the messagebox.

So my question is: Is there a way to make sure the main script continues even while the question is unanswered?

 

 

Link to comment
Share on other sites

1) Gui create

2) Gui set stat

2A) SW_HIDE

2B) SW_SHOW

3) Condition

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


$askexit = 1 ;set to 0 to exit without question

global $idno, $idexit,$msgboxgui

    $title = "Exit?"
    $text = "Are you sure you want to exit?"
    $msgboxgui = GUICreate($title, 100, 100, -1, -1, -1, $WS_EX_TOPMOST)
    GUICtrlCreateLabel($text, 10, 10)
    $idno = GUICtrlCreateButton("No", 50, 50)
    $idexit = GUICtrlCreateButton("exit", 10, 50)

HotKeySet("{ESC}", "Terminate")

While True
    ConsoleWrite("Running" & @CRLF)
    Sleep(50)
    Switch GUIGetMsg()
        Case $idno
            GUISetState(@SW_HIDE, $msgboxgui)
        Case $idexit
            leave()
    EndSwitch
WEnd

Func leave()
    ;some more cleanup commands...
    Exit 0
EndFunc   ;==>leave

Func askexit()
    ; Display the GUI.
    GUISetState(@SW_SHOW, $msgboxgui)
EndFunc   ;==>askexit

Func Terminate()
    If $askexit = 1 Then
        askexit()
    Else
        leave()
    EndIf
EndFunc   ;==>Terminate

it works, but it is a bit unresponsive because of the sleep...as i need to sleep in my script i will have to find some workaround...

Edited by Allow2010
Link to comment
Share on other sites

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#include <Timers.au3>

$askexit = 1 ;set to 0 to exit without question

Global $idno, $idexit, $msgboxgui

$title = "Exit?"
$text = "Are you sure you want to exit?"
$msgboxgui = GUICreate($title, 200, 100, -1, -1, -1, $WS_EX_TOPMOST)
GUICtrlCreateLabel($text, 10, 10)
$idno = GUICtrlCreateButton("No", 50, 50)
$idexit = GUICtrlCreateButton("exit", 10, 50)
;GUISetState(@SW_SHOW, $msgboxgui)

HotKeySet("{ESC}", "Terminate")

$timer = _Timer_SetTimer($msgboxgui, 50, "runner")

While True
    Switch GUIGetMsg()
        Case $idno, $GUI_EVENT_CLOSE
            GUISetState(@SW_HIDE, $msgboxgui)
        Case $idexit
            leave()
    EndSwitch
WEnd

Func runner($hWnd, $iMsg, $iIDTimer, $iTime)
    ConsoleWrite("Running" & @CRLF)
EndFunc   ;==>runner

Func leave()
    ;some more cleanup commands...
    _Timer_KillTimer($msgboxgui, $timer)
    Exit 0
EndFunc   ;==>leave

Func Terminate()
    If $askexit = 1 Then
        GUISetState(@SW_SHOW, $msgboxgui)
    Else
        leave()
    EndIf
EndFunc   ;==>Terminate

this one works OK, just in case someone needs something like this...

Edited by Allow2010
Link to comment
Share on other sites

You can also use AdlibRegister instead of a timer :

#Include <GUIConstantsEx.au3>
#Include <WindowsConstants.au3>

Global $hExitDialog, $id_ExitYes, $id_ExitNo
HotKeySet("{ESC}", "Terminate")

_ExitDialog()

AdlibRegister("_AdlibRunner", 500)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE, $id_ExitNo
            GUISetState(@SW_HIDE, $hExitDialog)
        Case $id_ExitYes
            Exit
    EndSwitch
WEnd

Func _AdlibRunner()
    ConsoleWrite("Running" & @CRLF)
EndFunc 

Func Terminate()
    GUISetState(@SW_SHOW, $hExitDialog)
EndFunc

Func _ExitDialog()
    $hExitDialog = GUICreate("Exit?", 242, 144, -1, -1, $WS_SYSMENU )
    GUISetFont(10, 400, 0, "Calibri")
    GUICtrlCreateLabel("Are you sure you want to exit?", 12, 26)

    GUICtrlCreateLabel("", 0, 70, 242, 74)
    GUICtrlSetBkColor(-1, 0xdddddd)
    GUICtrlSetState(-1, $GUI_DISABLE)

    $id_ExitYes = GUICtrlCreateButton("Yes", 42, 80, 89, 26)
    $id_ExitNo = GUICtrlCreateButton("No", 140, 79, 89, 26)
EndFunc
Link to comment
Share on other sites

FYI:

I did a few test, and for some reason unknown, my script is less responsive when using AdlibRegister .

Not sure if there is a technical reason that would explain it. I used the same timing and code, just repleced the functions.

Using Adlib register i sometimes have to click severltimes on the exit button until it reacts...never hat this with settimer.

So i will stay with _Timer_SetTimer for now.

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...