Jump to content
Sign in to follow this  
Prab

[Solved] Gui Interrupt

Recommended Posts

Prab

Hi,

I just switched one of my programs from Message-loop to On-event style to make it interruptible. I still can't get my "Abort" button to work. The hot key works.

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

$Button4 = GUICtrlCreateButton("Abort!", 120, 152, 89, 73, 0)
GUICtrlSetOnEvent($Button4, "Terminate")

Any suggestions?

Edited by Prab

Share this post


Link to post
Share on other sites
martin

Hi,

I just switched one of my programs from Message-loop to On-event style to make it interruptible. I still can't get my "Abort" button to work. The hot key works.

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

$Button4 = GUICtrlCreateButton("Abort!", 120, 152, 89, 73, 0)
GUICtrlSetOnEvent($Button4, "Terminate")

Any suggestions?

Presumably you are trying to abort when the script is executing a function called by a previous interrupt. If that is the case then if a function is started by an event then no other events will be dealt with untill that function returns. Sometimes you can get round this by having the function set a global variable and returning, and have the main script operate in response to that variable being set.

Hopefiully this explains what I mean.

Opt("GUIOnEventMode", 1)
GuiCreate(...

.
.
$Button1 = GUICtrlCreateButton("Button1", 112, 200, 75, 25, 0)
GUICtrlSetOnEvent(-1, "Button1Click");We really want this to be Func1 but if we do that button 2 won't exit the program until Func1 is finished.
$Button2 = GUICtrlCreateButton("Button2", 248, 192, 75, 25, 0)
GUICtrlSetOnEvent(-1, "Button2Click")


$b = false
While 1
    Sleep(100)
    if $b then Func1();if we call func1 from here then the button2 event can be executed and interupt it.
    
WEnd

Func Button1Click()
$b = true;  this way we quickly return from the function and can deal with another event
EndFunc

Func Button2Click()
Exit
EndFunc

Func Func1();something that takes a long time to execute
$b = False
for $n = 1 to 300
    ConsoleWrite($n & @CRLF)
    sleep(300)
Next

    
EndFunc

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Prab

Thanks you so much! Its sort of an ugly hack, but I can see why it needs to be like that. I think it would be better if the Events could interrupt each other and build up a stack. That makes more sense for an event driven system.

Share this post


Link to post
Share on other sites
jackchen

I think it would be better if the Events could interrupt each other and build up a stack. That makes more sense for an event driven system.

I think so too!

An example as below:Preass "Run Notepad" to launch notepad.Then the button "test" will not available untill you close notepad.

#include <GUIConstantsEx.au3>
opt("GUIONEVENTMODE", 1)
GUICreate("Test", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUICtrlCreateButton("Run NotePad", 60, 150)
GUICtrlSetOnEvent(-1,"_RunNotePad")
GUICtrlCreateButton("Test", 200, 150)
GUICtrlSetOnEvent(-1,"_Test")
GUISetState()
While 1
    Sleep(100)
WEnd

func _RunNotePad()
    RunWait("notepad.exe")
EndFunc

func _Test()
    MsgBox(0,"", "Test") ; Still not called....
EndFunc

Func _Exit()
    Exit
EndFunc

AutoHotkey's events can interrupt each other, though.See below from AutoHotkey helpfile:

Although AutoHotkey doesn't actually use multiple threads, it simulates some of that behavior: If a second thread is started -- such as by pressing another hotkey while the previous is still running -- the current thread will be interrupted (temporarily halted) to allow the new thread to become current. If a third thread is started while the second is still running, both the second and first will be in a dormant state, and so on.

When the current thread finishes, the one most recently interrupted will be resumed, and so on, until all the threads finally finish. When resumed, a thread's settings for things such as ErrorLevel and SendMode are automatically restored to what they were just prior to its interruption; in other words, a thread will experience no side-effects from having been interrupted (except for a possible change in the active window).

A single script can have multiple simultaneous MsgBox, InputBox, FileSelectFile, and FileSelectFolder dialogs. This is achieved by launching a new thread (via hotkey, timed subroutine, custom menu item, etc.) while a prior thread already has a dialog displayed.

Example:

Gui, Add, Button,  x60 y150 gRunNotePad, Run NotePad
Gui, Add, Button, x200 y150 gTest, Test
Gui, Show, w300 h200
Return

GuiClose:
ExitApp
Return

RunNotePad:
RunWait, notepad.exe
Return

Test:
MsgBox, Test
Return
Edited by jackchen

Share this post


Link to post
Share on other sites
jackchen

An example as below:Preass "Run Notepad" to launch notepad.Then the button "test" will not available untill you close notepad.

Now I have a simple solution:

#include <GUIConstantsEx.au3>
opt("GUIONEVENTMODE", 1)
GUICreate("Test", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUICtrlCreateButton("Run NotePad", 60, 150)
GUICtrlSetOnEvent(-1,"_RunNotePad")
GUICtrlCreateButton("Test", 200, 150)
GUICtrlSetOnEvent(-1,"_Test")
GUISetState()
While 1
    Sleep(100)
WEnd

func _RunNotePad()
    AdlibRegister("_DoRunNotePad", 10) ; leave the task to a timer and return as soon as possible
EndFunc

Func _DoRunNotePad() ; This routine can be interrupted by Gui Events
    AdlibUnRegister("_DoRunNotePad") ; Unregister the timer
    RunWait("notepad.exe")
EndFunc

func _Test()
    MsgBox(0,"", "Test")
EndFunc

Func _Exit()
    Exit
EndFunc
Edited by jackchen

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.