Sign in to follow this  
Followers 0
Rugbee

Run Fuction on GUI Load - On Event mode

6 posts in this topic

#1 ·  Posted (edited)

Hi,

I am building a small app to help our client manage Windows Media Services from a central point. We have 9 servers using on-demand publishing points. I have built a GUI interface using AutoIT and also used an autoit script to stop / start or verify status of the Media Services service on each of these servers. The start / stop script works great but as an added bonus I wanted to check the service status on each server when the GUI loads.

I have completed a function that does this and it will change the text color of a radio button (red if service is stopped / green if running) that correspondes to each server when the function completes. I can call the function easily when I have the user click on a buttion for starting or stopping the service. I would like the fucntion to run when the GUI first opens.

I have added my fuction to be called within the While loop (as I am using on event mode for GUI) and increased the sleep time but I don't think this is the best solution. I have looked at using the GUICtrlSetState and GUICtrlSetOnEvent when the GUI was created but I have had no luck. I can get it to work when maximizing the window or on a click, but I just want it to run the function once when the GUI first opens and that's it.

Is this possible using on event mode? Any help is much appreciated.

Edited by Rugbee

Share this post


Link to post
Share on other sites



You could just call the function you are using in "GUICtrlSetOnEvent" then have a loop inside that function to just keep checking what you need.

Something like.

MyFunction()
$btn = GUICtrlCreateButton("button", 20, 80, 50, 20)
GUICtrlSetOnEvent($btn, "MyFunction")

Func MyFunction()
    While 1
        
        ;put your code here with a way to get out of loop
    
    WEnd
EndFunc

You really only need the button to start the function if you plan to stop the function loop and need a way to restart.

Kohr

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks, unfortunately I don't want the function to load by a button press, I would like it to load when the GUI is created. for example I would like to use GUICtrlSetonEvent or GUISetOnEvent right after the main window is created in the following chunk of code.

CODE
$MainWindow = GuiCreate("Media Services Control Applet", 400, 400)

GUISetOnEvent($GUI_EVENT_CLOSE, "_AppClose")

I have already used GUICtrlSetonEvent(-1,"_MyFunctionstartService") for a start service button and the same for a stop service button. I have also called my fuction within the main loop for an "On Event Mode" GUI, but I had to increase the sleep time as it will contine to run my function while the GUI is open as in the following code chunk:

CODE
While 1

_WMStatusCheck()

Sleep(50000000)

Wend

I only want the function to run once on GUI load without user interaction.

Edited by Rugbee

Share this post


Link to post
Share on other sites

Like this?

#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)


$MainWindow = GuiCreate("Media Services Control Applet", 400, 400)
$label = GUICtrlCreateLabel("test", 20, 80, 50, 20)

GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "_AppClose")
_MyFunctionstartService()
While 1
    Sleep(200)
WEnd

Func _MyFunctionstartService()
    While 1
        Sleep(2000)
        GUICtrlSetBkColor($label,0x0000ff)
        Sleep(2000)
        GUICtrlSetBkColor($label,0x00ff00)
    WEnd
EndFunc


Func _AppClose()
    Exit
EndFunc

Kohr

Share this post


Link to post
Share on other sites

Man sometimes I look for the hardest way to find the solution. Thanks for the answer. I wasn't calling the function in the right place. When I called the function outside of the While loop I was putting it right under the GUI Create command. When I moved it just before the while loop (after I created the GUI and radio buttons etc... that get evalutated in the function) it worked just as I wanted it to. It loads only once on startup.

Thanks Kohr, much appreciated. Problem solved. :">

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