Sign in to follow this  
Followers 0
Allow2010

How to use hotkey to exit without blocking?!

10 posts in this topic

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?

 

 

Share this post


Link to post
Share on other sites



Instead of msgbox why don't you use guicreate?


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

unsure if this can help...a loop for the guimsg would block also...mayb i can try with eventmode...

Share this post


Link to post
Share on other sites

You can display a gui and still have your script running in the background.


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

1) Gui create

2) Gui set stat

2A) SW_HIDE

2B) SW_SHOW

3) Condition

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

#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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

#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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

ahh, good, especiall if there is no gui.-)

Share this post


Link to post
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.

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