Jump to content
Sign in to follow this  
Paulexander

Trying to create a Hotkey that can stop a Loop

Recommended Posts

Paulexander

I am trying to create a HotKey that can stop a loop. It worked for me before, and even compiled under a previous version (3.2.0.0??), but after I upgraded, when I test it I get caught on my bad syntax.

The purpose of this is to make a little service control utility. The reason I want to be able to cancel the execution is sometimes the services are just belligerent and do not ever come up. I would like to provide a method for my users to Escape out, but not close the whole utility.

I also tried skipping the HotKey altogether, and put in "If Function_IsPressed ("26") Then Exit", (included misc.au3) and got absolutely nowhere.

If there is a better way to do it, what am I missing?

CODE

#include <GUIConstants.au3>

AutoItSetOption ( "TrayAutoPause", 0)

GUICreate ("RA Control Utility", 200, 200)

GUICtrlCreateLabel ("Reading Assistant", 42, 8, $SS_CENTERIMAGE)

GUICtrlCreateLabel ("Control Utility", 52, 28, $SS_CENTERIMAGE)

GUICtrlSetFont (3, 10, 700)

GUICtrlSetFont (4, 10, 700)

$STOP = GUICtrlCreateButton ("Stop All RA Services", 45, 70, 110)

$RESTART = GUICtrlCreateButton ( "Stop and Restart RA Services", 22, 115, 155)

$EXIT = GUICtrlCreateButton ( "Exit", 85, 160, 30)

GUISetState ()

; provides ability to cancel utility if it stalls

HotKeySet("{Esc}", "captureEsc")

Func captureEsc()

SplashOff ()

$MSG1 = MsgBox (0, "Reading Assistant Control Utility", "Cancel?")

If $MSG1 = 1 Then ExitLoop ; <- This is where I get hung up, dag gummit!!

EndFunc

While 1

$msg = GUIGetMsg()

While $msg = $STOP

SplashTextOn ( "Reading Assistant Control Utility", "Stopping Services - Please Wait. ( Esc to Cancel )", 100, 100,-1,-1,-1, "Arial", -1, 800)

RunWait (@ComSpec & " /c " & 'SC stop SLMySQLRA', "", @SW_HIDE)

ProcessWaitClose ("mysqld-nt.exe")

RunWait (@ComSpec & " /c " & 'SC stop SLTomcatRA', "", @SW_HIDE)

ProcessWaitClose ("tomcat5.exe")

SplashOff ()

$MSG2 = MsgBox (0, "Reading Assistant Control Utility", "Services Have Been Stopped")

If $MSG2 = 1 Then

ExitLoop

EndIf

WEnd

While $msg = $RESTART

SplashTextOn ( "Reading Assistant Control Utility", "Restarting Services - Please Wait. ( Esc to Cancel )", 100, 100,-1,-1,-1, "Arial", -1, 800)

RunWait (@ComSpec & " /c " & 'SC stop SLMySQLRA', "", @SW_HIDE)

RunWait (@ComSpec & " /c " & 'SC start SLMySQLRA', "", @SW_HIDE)

ProcessWait ("mysqld-nt.exe")

RunWait (@ComSpec & " /c " & 'SC stop SLTomcatRA', "", @SW_HIDE)

RunWait (@ComSpec & " /c " & 'SC start SLTomcatRA', "", @SW_HIDE)

ProcessWait ("tomcat5.exe")

;SplashOff ()

$MSG3 = MsgBox (0, "Reading Assistant Control Utility", "Services Have Been Restarted")

If $MSG3 = 1 Then

ExitLoop

EndIf

WEnd

While $msg = $EXIT

Exit

WEnd

If $msg = $GUI_EVENT_CLOSE Then Exit

WEnd

Thanks

-Paul

Share this post


Link to post
Share on other sites
ResNullius

What you want to do is have your Hotkey Function set a variable flag that your loop can check during execution

So something like:

Global $StopServices = 0

; provides ability to cancel utility if it stalls
HotKeySet("{Esc}", "captureEsc")
Func captureEsc()
SplashOff ()
$MSG1 = MsgBox (0, "Reading Assistant Control Utility", "Cancel?")
If $MSG1 = 1 then $StopServices = 1
EndFuncoÝ÷ ØÚ-²êÞ­¶÷Þ­éÜz+,ºh±çm¡·zÜzp¥É"az«¨µ+h¦«¨µ»­¶ÚÚkzË"¬qª^nër¢êÖ)^.)².ÖÞ²¶§X¤y«­¢+Ù]¡¥±Ä($ÀÌØíµÍôU%Ñ5Í ¤(%%ÀÌØíMѽÁMÉÙ¥ÌôÄÑ¡¸($$í¼å½ÕÈÍÁչѥ½¹Ì¡É($$ÀÌØíMѽÁMÉÙ¥ÌôÀìÉÍÐÀÌØíMѽÁMÉÙ¥ÌѼÀ(%¹%($)MÝ¥Ñ ÀÌØíµÍ(%
ÍÀÌØíMQ=@($$%MÁ±Í¡QáÑ=¸ ÅÕ½ÐíI¥¹ÍÍ¥ÍѹÐ
½¹Ñɽ°UÑ¥±¥ÑäÅÕ½Ðì°ÅÕ½ÐíMѽÁÁ¥¹MÉ٥̴A±Í]¥Ð¸ ÍѼ
¹°¤ÅÕ½Ðì°ÄÀÀ°ÄÀÀ°´Ä°´Ä°´Ä°ÅÕ½ÐíÉ¥°ÅÕ½Ðì°´Ä°àÀÀ¤($$%IÕ¹]¥Ð¡
½µMÁµÀìÅÕ½Ðì½ÅÕ½ÐìµÀìÌäíMÍѽÀM15åME1IÌäì°ÅÕ½ÐìÅÕ½Ðì°M]}!%¤($$%AɽÍÍ]¥Ñ
±½Í ÅÕ½ÐíµåÍűµ¹Ð¹áÅÕ½Ðì¤($$%IÕ¹]¥Ð¡
½µMÁµÀìÅÕ½Ðì½ÅÕ½ÐìµÀìÌäíMÍѽÀM1Q½µÑIÌäì°ÅÕ½ÐìÅÕ½Ðì°M]}!%¤($$%AɽÍÍ]¥Ñ
±½Í ÅÕ½ÐíѽµÐÔ¹áÅÕ½Ðì¤($$%MÁ±Í¡= ¤($$$ÀÌØí5MÈô5Í   ½à À°ÅÕ½ÐíI¥¹ÍÍ¥ÍѹÐ
½¹Ñɽ°UÑ¥±¥ÑäÅÕ½Ðì°ÅÕ½ÐíMÉÙ¥Ì!Ù ¸MѽÁÁÅÕ½Ðì¤($$$í%ÀÌØí5MÈôÄQ¡¸($%
ÍÀÌØíIMQIP($$%MÁ±Í¡QáÑ=¸ ÅÕ½ÐíI¥¹ÍÍ¥ÍѹÐ
½¹Ñɽ°UÑ¥±¥ÑäÅÕ½Ðì°ÅÕ½ÐíIÍÑÉÑ¥¹MÉ٥̴A±Í]¥Ð¸ ÍѼ
¹°¤ÅÕ½Ðì°ÄÀÀ°ÄÀÀ°´Ä°´Ä°´Ä°ÅÕ½ÐíÉ¥°ÅÕ½Ðì°´Ä°àÀÀ¤($$%IÕ¹]¥Ð¡
½µMÁµÀìÅÕ½Ðì½ÅÕ½ÐìµÀìÌäíMÍѽÀM15åME1IÌäì°ÅÕ½ÐìÅÕ½Ðì°M]}!%¤($$%IÕ¹]¥Ð¡
½µMÁµÀìÅÕ½Ðì½ÅÕ½ÐìµÀìÌäíMÍÑÉÐM15åME1IÌäì°ÅÕ½ÐìÅÕ½Ðì°M]}!%¤($$%AɽÍÍ]¥Ð ÅÕ½ÐíµåÍűµ¹Ð¹áÅÕ½Ðì¤($$%IÕ¹]¥Ð¡
½µMÁµÀìÅÕ½Ðì½ÅÕ½ÐìµÀìÌäíMÍѽÀM1Q½µÑIÌäì°ÅÕ½ÐìÅÕ½Ðì°M]}!%¤($$%IÕ¹]¥Ð¡
½µMÁµÀìÅÕ½Ðì½ÅÕ½ÐìµÀìÌäíMÍÑÉÐM1Q½µÑIÌäì°ÅÕ½ÐìÅÕ½Ðì°M]}!%¤($$%AɽÍÍ]¥Ð ÅÕ½ÐíѽµÐÔ¹áÅÕ½Ðì¤($$$íMÁ±Í¡= ¤($$$ÀÌØí5MÌô5Í  ½à À°ÅÕ½ÐíI¥¹ÍÍ¥ÍѹÐ
½¹Ñɽ°UÑ¥±¥ÑäÅÕ½Ðì°ÅÕ½ÐíMÉÙ¥Ì!Ù ¸IÍÑÉÑÅÕ½Ðì¤($$$($%
ÍÀÌØía%P=HÀÌØíU%}Y9Q}
1=M($$%á¥Ð(%¹MÝ¥Ñ )]¹

Share this post


Link to post
Share on other sites
Paulexander

Thanks for your help, but I just realized I messed up my description a bit...

What I want it to do is, while either the Stop or Restart is executing, have the ability to Escape out and stop trying. That's where the potential stalls occur.

I understand what you are suggesting to me, and the switch seems to make more sense, but it's just not doing it. I will try again after sleep.

Share this post


Link to post
Share on other sites
spudw2k

I think the prob is RunWait() pauses the script. using ResNullius' flag method, would something like this work for you?

Switch $msg
    Case $STOP
        _Stop
    Case $RESTART
        _Restart   
    Case $EXIT OR $GUI_EVENT_CLOSE
        Exit
EndSwitch
        
Func _Stop()
    SplashTextOn ( "Reading Assistant Control Utility", "Stopping Services - Please Wait. ( Esc to Cancel )", 100, 100,-1,-1,-1, "Arial", -1, 800)
    If _SCCommand("SC stop SLMySQLRA","mysqld-nt.exe") Then
        _SCCommand("SC stop SLTomcatRA","tomcat5.exe")
    EndIf
    SplashOff ()
    $MSG2 = MsgBox (0, "Reading Assistant Control Utility", "Services Have Been Stopped")
EndFunc

Func _Restart()
    SplashTextOn ( "Reading Assistant Control Utility", "Restarting Services - Please Wait. ( Esc to Cancel )", 100, 100,-1,-1,-1, "Arial", -1, 800)
    If _SCCommand("SC stop SLMySQLRA","mysqld-nt.exe") Then
        If _SCCommand("SC start SLMySQLRA","mysqld-nt.exe") Then
            If _SCCommand("SC stop SLTomcatRA","tomcat5.exe") Then
                _SCCommand("SC start SLTomcatRA","tomcat5.exe")
            EndIf
        EndIf
    EndIf
    SplashOff()
    $MSG3 = MsgBox (0, "Reading Assistant Control Utility", "Services Have Been Restarted")
EndFunc

Func _SCCommand($command,$process)
    $pid = Run(@Comspec & " /c " & $command, "", @SW_HIDE)
    If StringInstr($command,"stop") Then
        Do
            If $StopServices = 0 Then 
                ProcessClose($pid)
                $StopServices = 1
                Return 0
            EndIf
            sleep(200)
        Until Not ProcessExists($process)
    Else
        Do
            If $StopServices = 0 Then 
                ProcessClose($pid)
                $StopServices = 1
                Return 0
            EndIf
            sleep(200)
        Until ProcessExists($process)       
    Return 1
EndFunc

Share this post


Link to post
Share on other sites
Paulexander

Thanks Guys, this is a lot to chew on. I just now got time today to take another swing at this, and I'm still not getting the behavior I want, but I get the impression these are good leads. I will keep trying tomorrow.

One thing I noticed, is that the line "Case $EXIT OR $GUI_EVENT_CLOSE" produces very odd behavior. The Screen gets drawn, but the minute I move the mouse, it disappears. If I split it into two lines it works just fine (except for the cancelling in mid function or loop)

Share this post


Link to post
Share on other sites
danielkza

Thanks Guys, this is a lot to chew on. I just now got time today to take another swing at this, and I'm still not getting the behavior I want, but I get the impression these are good leads. I will keep trying tomorrow.

One thing I noticed, is that the line "Case $EXIT OR $GUI_EVENT_CLOSE" produces very odd behavior. The Screen gets drawn, but the minute I move the mouse, it disappears. If I split it into two lines it works just fine (except for the cancelling in mid function or loop)

The Corret Is:

Case $EXIT,$GUI_EVENT_CLOSE

Share this post


Link to post
Share on other sites
ResNullius

The Corret Is:

Case $EXIT,$GUI_EVENT_CLOSE
I should have known better :)

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  

×