Sign in to follow this  
Followers 0
Klexen

Force program to close if broken

11 posts in this topic

#1 ·  Posted (edited)

I was wondering if you can force your program to terminate (without use of killing process with task manager) if it gets hung up. In some of my functions that run I have WinWaitActive command or similar, and lets say for whatever reason that window could never be active, the program will hang until it is and you can't do anything. Is there a way to set a hotkey to force your program to terminate, even if it's stuck in a pause state from function not being completed.

For instance, Say I want my program to run several different functions at the click of the button, and while those functions are running, I want to blockinput. Then after functions run enable input. Well if the program hangs during a function, input would be blocked for good.

Any ideas? Did what I say make sense? Heh.

Edited by Klexen

Share this post


Link to post
Share on other sites



Look at WinClose and WinKill.


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Yeah but will those work even if program is stuck in a function? Or would I need to run a seperate script to set a hotkey to kill the program?

Share this post


Link to post
Share on other sites

As long as your script isn't hung up, all of the above should close your app.


IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]

Share this post


Link to post
Share on other sites

Yeah but will those work even if program is stuck in a function? Or would I need to run a seperate script to set a hotkey to kill the program?

You can write your functions so that they will never get stuck - they always have a timeout and they pass window info to an "Exit function" that closes your window of interest and unblocks user inputs... then it can exit the script of just display an error MsgBox.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Hi,

or use ablibEnable to check every x ms for something.

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

There's a function for that (sorry not to mention the author, but I have it on my snipplets library):

If _NotResponding("TITLE HERE", "TEXT HERE[OPTIONAL]", 1) Then; The last parameter indicates whether you want to close the hung app or not.
     MsgBox(0, "", "Hung Application, closing app now.")
Else
     MsgBox(0, "", "Application running as intended.")
EndIf
 
Func _NotResponding($title, $text, $closeIfHung = 0)
     $hWnd = WinGetHandle($title, $text)
     If $hWnd == "" Then
          MsgBox(0, "Error", "Could not find window")
          Exit
     EndIf
     $retArr = DllCall("user32.dll", "int", "IsHungAppWindow", "hwnd", $hWnd)
     If @error == 0 Then
          If $retArr[0] == 1 Then
               If $closeIfHung Then
                    ProcessClose(WinGetProcess($title, $text))
               EndIf
               Return 1
          EndIf
     Else
          Return 0
     EndIf
EndFunc   ;==>_NotResponding

[topic="51913"]Restrict USB Storage usage to group membership[/topic] * [topic="48699"]Using nircmd library[/topic] * Some admin notes

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

The winwaitactive, winwait commands have a timeout period option, so why don't you set a timeout period and always check on the next line to see if the window is active, visible ect, if not then come out of the function and do whatever else you need to do to get to where you want to be.

Edit: Added crude example

Do 
    $exists = WaitForWindow("Untitled - Notepad","",10)
    If $exists = 0 then 
        Msgbox(0,"Failure","Notepad doesn't exist")
        Run ("Notepad.exe")
    Else
        Msgbox(0,"Success","Notepad exists")
    EndIf
Until $exists = 1


Func WaitForWindow($win,$Txt="",$timeout=0)
    
    WinWait($win,$Txt,$Timeout)
    
    If WinExists($win,$Txt) then 
        Return 1
    Else
        Return 0
    EndIf
    
EndFunc

Edit2: You can do the same with this code and not using a separate function, but the example above showed you how to come out of your function

Do 
    $exists = WinWait("Untitled - Notepad","",10)
    If $exists = 0 then 
        Msgbox(0,"Failure","Notpead doesn't exist")
        Run ("Notepad.exe")
    Else
        Msgbox(0,"Success","Notpead exists")
    EndIf
Until $exists = 1
Edited by ChrisL

Share this post


Link to post
Share on other sites

Thanks guys! I got somethin workin to do it now! :whistle:

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