Jump to content

Best way of having two loops at once?


Recommended Posts

I'm hoping someone out there can shed some light on this for me.

I built a GUI that checks on computer systems that are online or offline, right now the user has to click a button to check, I would like the update to happen automatically. I can do this and it works fine as long as all systems are online, however if a computer is offline then the responsiveness of the GUI suffers.

Other than increasing the intervals between checks, I believe I might have to build the code into two functions, one that takes care of the GUI and one that does the pinging of the systems. This way the GUI only has to look at a variable that is updated by the pinging but never has to slow down for the ping to take place, however I cant figure out how to share the information from the second function with the first so it can display an online or offline message to the user.

Any ideas on how to go about doing this would be great.

Sorry for the quick sample, I don't have the actual code with me so I built this for testing different ways of making it work, so far nothing is working correctly, I'm pretty green and have spent a lot of hours online looking for a solution but I have not been able to solve my problem.

#include <GUIConstantsEx.au3>

Local $ping1, $ping2, $i

_Main()

Func _Main()

GUICreate("Test", 500, 300)

$okbutton = GUICtrlCreateButton("OK", 70, 50, 60)

GUISetState(@SW_SHOW)

$i = 1

While 1

$msg = GUIGetMsg()

$i = $i + 1

if $i = 50 Then

$ping1 = Ping("system_abc", 250)

if not $ping1 = 0 Then

GUICtrlCreateLabel("Online", 30, 100)

Else

GUICtrlCreateLabel("Offline", 30, 100)

EndIf

EndIf

if $i = 100 then

$ping2 = Ping("system_123", 250)

if not $ping2 = 0 Then

GUICtrlCreateLabel("Online", 30, 130)

Else

GUICtrlCreateLabel("Offline", 30, 130)

EndIf

$i = 1

EndIf

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $okbutton

MsgBox(0, "", "some code here for the ok button")

EndSelect

WEnd

EndFunc

Link to comment
Share on other sites

I believe I might have to build the code into two functions

I think your on the right track there- in one program i wrote i had to deal with a realtime issue while updating an output file.

The best workaround was to use a timer interupted function to poll for the results from the realtime feed to avoid similar problems you describe.

Perilous to all of us are the devices of an art deeper than we ourselves possess.

Link to comment
Share on other sites

SIone,

Thanks for the info, my problem is when I add another function that the second function doesn't do anything. When I add a call to the function then it does start executing the function however I run into even more problems.

The second function wont allow anything else to work properly and it still isn't updating the $ping1 and $ping2. Do you have example code of the way you solved the problem?

Link to comment
Share on other sites

SIone,

Thanks for the info, my problem is when I add another function that the second function doesn't do anything. When I add a call to the function then it does start executing the function however I run into even more problems.

The second function wont allow anything else to work properly and it still isn't updating the $ping1 and $ping2. Do you have example code of the way you solved the problem?

i do but since your having an issue with your program it will be easyier to help you if you can post your code

Perilous to all of us are the devices of an art deeper than we ourselves possess.

Link to comment
Share on other sites

This is the best I could come-up with. If you are going to continue to "ping" often your script will suffer

#include <GUIConstantsEx.au3>

Global $iPing


GUICreate("Test", 500, 300)
$lbl1 = GUICtrlCreateLabel("Online", 30, 100)
$lbl2 = GUICtrlCreateLabel("Online", 30, 130)
$okbutton = GUICtrlCreateButton("OK", 70, 50, 60)
GUISetState(@SW_SHOW)

AdlibEnable("Pinger", 5000); set how often you want 5000 = 5 seconds

While 1
    $msg = GUIGetMsg()

    Select

        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop

        Case $msg = $okbutton
            Do_A_Function()

    EndSelect
WEnd

; ------------------------------- FUNCTIONS -----------------------------------------------

Func Do_A_Function()
    MsgBox(0, "", "some code here for the ok button")
    ; do other stuff
EndFunc   ;==>Do_A_Function

Func Pinger()
    $iPing = Not $iPing
    
    If $iPing Then
        $ping1 = Ping("system_abc", 250)
        If Not $ping1 = 0 Then
            GUICtrlSetData($lbl1, "OnLine")
        Else
            GUICtrlSetData($lbl1, "OffLine")
        EndIf
    Else
        $ping2 = Ping("system_123", 250)
        If Not $ping2 = 0 Then
            GUICtrlSetData($lbl2, "OnLine")
        Else
            GUICtrlSetData($lbl2, "OffLine")
        EndIf
        $i = 1
    EndIf
EndFunc   ;==>Pinger

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Valuater,

Well that does work, but is there any way that Func Pinger() run independently without being called every x number of seconds? What I originally imagined was that every x amount of time the first function simply just reads the status rather than actually run that function.

Thanks for the help though, I have a better understanding of functions from your example.

Link to comment
Share on other sites

I think you can run the ping function independently by making it another script in itself. It could write the ping results to a file after each time it iterates. Your original script would read the file results every so often and display it. The more hosts you ping the longer you wait for updates to be shown. Unfortunately I do not know how to pass the array from one running script to another without using a file.

Check out http://www.tallsoft.com/pingmonitor.htm if all you want is a display of items up/down on your network

stumppc

Link to comment
Share on other sites

I thought about using a separate file, unfortunately the computers being pinged aren't static so it would make using a separate file much more difficult..

as for using AdlibEnable() for calling two functions I tried that but it stalls out the app, I'm pretty new to autoit so I may be doing something wrong, any chance for a small sample I could use for a reference?

Thanks

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...