Jump to content
Sign in to follow this  
J03Z

Repeating an action

Recommended Posts

J03Z

Hello,

I am needing a little assistance with a particular area of coding. Before I start, I must say that I have completed all tutorials presented on autoit via www.autoit.com. I have also checked the help file which is very informative but in this case I need a little more direction.

The idea of the program is to ping a specified computer and display wether it is online or offline. In the event the computer goes offline, I would like to have the display change to offline and vice versa.

I have the ping button already coded to return online or offline status. I just need to know if looping would be the best method to have it contsantly check wether the computer is still offline or online. Please give me a little direction and possibly a sample of coding that may help.

Thanks,

Share this post


Link to post
Share on other sites
martin

Hello,

I am needing a little assistance with a particular area of coding. Before I start, I must say that I have completed all tutorials presented on autoit via www.autoit.com. I have also checked the help file which is very informative but in this case I need a little more direction.

The idea of the program is to ping a specified computer and display wether it is online or offline. In the event the computer goes offline, I would like to have the display change to offline and vice versa.

I have the ping button already coded to return online or offline status. I just need to know if looping would be the best method to have it contsantly check wether the computer is still offline or online. Please give me a little direction and possibly a sample of coding that may help.

Thanks,

For a job that needs to be done repeatedly, and for a neat way to separate it from other code unconnected with that job, have a look at AdlibRegister rather than using a loop.

EDIT: Hi Jo3z! Welcome to the AutoIt forums.

Edited by martin

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
J03Z

Hey,

Thanks Martin, this worked like a charm.

Share this post


Link to post
Share on other sites
J03Z

For a job that needs to be done repeatedly, and for a neat way to separate it from other code unconnected with that job, have a look at AdlibRegister rather than using a loop.

EDIT: Hi Jo3z! Welcome to the AutoIt forums.

Martin, just a follow up. I am able to get it to repeat the function without locking the gui up but there is one other problem I wanted to see if you could point out. I want it to display offline when the computer goes offline. I tried to adlibunregister()and point to offline function but still no go. My code may have a lot of un necessary code. Please take a look and make any recomendations. Thanks

#include <ButtonConstants.au3>

#include <EditConstants.au3>

#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=e:\autoit\command kit\commander.kxf

$Commander = GUICreate("Commander v1.0", 264, 201, 318, 193)

$File = GUICtrlCreateMenu("&File")

$Exit = GUICtrlCreateMenuItem("Exit", $File)

$View = GUICtrlCreateMenu("&View")

$Sview = GUICtrlCreateMenuItem("Simple View", $View)

$Aview = GUICtrlCreateMenuItem("Advanced View", $View)

$Help = GUICtrlCreateMenu("&Help")

$Usage = GUICtrlCreateMenuItem("How To Use Commander", $Help)

$Aboutkit = GUICtrlCreateMenuItem("About Commander", $Help)

$Group1 = GUICtrlCreateGroup("", 8, 8, 244, 161)

$Instructions = GUICtrlCreateLabel("Enter Hostname or IP", 20, 30, 105, 17)

$InputData = GUICtrlCreateInput("", 16, 52, 113, 21)

$btn_Ping = GUICtrlCreateButton("Ping", 16, 80, 65, 20, BitOR($BS_DEFPUSHBUTTON, $BS_PUSHLIKE))

GUICtrlSetTip($btn_Ping, "This Pings Remote Host")

$btn_Look = GUICtrlCreateButton("Nslookup", 16, 108, 65, 20, $WS_GROUP)

GUICtrlSetTip($btn_Look, "Finds Host Name For Give IP/ IP For Given Host Name")

$btn_RDP = GUICtrlCreateButton("Remote Into Machine", 16, 136, 113, 25, $WS_GROUP)

GUICtrlSetTip($btn_RDP, "Remotely Connects To Host/IP Specified")

$Radio_SMS = GUICtrlCreateRadio("SMS", 144, 136, 49, 25)

$Radio_RDP = GUICtrlCreateRadio("RDP", 200, 136, 49, 25)

$Ping_Dash = GUICtrlCreateLabel("-", 96, 83, 9, 20)

GUICtrlSetFont($Ping_Dash, 10, 800, 0, "MS Sans Serif")

$Look_Dash = GUICtrlCreateLabel("-", 96, 110, 9, 20)

GUICtrlSetFont($Look_Dash, 10, 800, 0, "MS Sans Serif")

$Label4 = GUICtrlCreateLabel("", 112, 83, 4, 4, $SS_CENTER)

$p_Status = GUICtrlCreateLabel("", 120, 80, 80, 20, BitOR($SS_CENTER, $WS_BORDER))

GUICtrlSetFont($p_Status, 8, 800, 4, "MS Sans Serif")

$n_Status = GUICtrlCreateLabel("", 120, 108, 113, 20, BitOR($SS_CENTER, $WS_BORDER))

GUICtrlCreateGroup("", -99, -99, 1, 1)

GUISetState(@SW_SHOW)

Dim $Commander_AccelTable[1][2] = [["^+{F21}", $Aboutkit]]

GUISetAccelerators($Commander_AccelTable)

#EndRegion ### END Koda GUI section ###

While 1 ; keeps the gui opening

$nMsg = GUIGetMsg() ; a listening event. stores what ever happens in $nmsg

Switch $nMsg

Case $GUI_EVENT_CLOSE ; if red x is clicked. close program

Exit

Case $btn_Ping ; if ping button is clicked perform the following.

Global $inputText = GUICtrlRead($InputData) ; takes texted entered into the text box and stores it in variable.

global $ping_info = Ping($inputText, 3000) ; ping data and stores it in a variable. The return value returns numbers

If @error = 0 And StringLen($inputText) < 1 Then ; if there is no error and there is no text entered, prompt

GUICtrlSetData($InputData, "Enter IP or Hostname !")

elseIf $inputText = "Enter Ip or Hostname !" Then ; if there is nothing entered, prompt.

GUICtrlSetData($InputData, "Enter IP or Hostname !")

GUICtrlSetDefColor($p_status)

ElseIf @error = 0 Then ;if there is no error, display online

AdlibRegister("_CheckOstatus", 5000)

Else

AdlibRegister("_CheckOFFstatus", 5000)

EndIf

Case $Radio_RDP ; If the radio_SMS or Radio_rdp is checked, then enable btn_RDP

If $Radio_RDP Or $Radio_SMS = $GUI_Checked Then

GUICtrlSetState($btn_RDP, $GUI_Enable)

EndIf

Case $Radio_SMS ; If the radio_SMS or Radio_rdp is checked, then enable btn_RDP

If $Radio_RDP Or $Radio_SMS = $GUI_Checked Then

GUICtrlSetState($btn_RDP, $GUI_Enable)

EndIf

Case $btn_RDP ; if the RDP button is clicked do the following below.

$rdpstatcheck = GUICtrlRead($Radio_RDP) ; stores current status of button into a variable

$smsstatcheck = GUICtrlRead($Radio_SMS) ; stores current status of button into a variable

$inputText = GUICtrlRead($InputData) & "{space}" & "{SPACE}" ; what ever is entered into text box, tape space bar twice and store this in a variable.

If $rdpstatcheck = 1 And StringLen($inputText) > 1 Then ; if rdp_radio is selected, do the following.

Run("mstsc.exe") ; runs the program.

Sleep(1000) ; waits 1 second.

ControlSend("Remote Desktop Connection", "", "Edit1", $inputText) ; send the following info and so on.

Sleep(800)

ControlSend("Remote Desktop Connection", "", "Button5", "{ENTER}")

Sleep(2000)

If WinActive("Remote Desktop Connection") Then

ControlSend("Remote Desktop Connection", "", "Button1", "{Space}")

Sleep(500)

ControlSend("Remote Desktop Connection", "", "Button1", "{Right}")

ControlSend("Remote Desktop Connection", "", "Button1", "{Enter}")

EndIf

EndIf

EndSwitch

WEnd

Func _CheckOstatus()

GUICtrlSetState($p_Status, $GUI_SHOW)

GUICtrlSetData($p_Status, "Online")

GUICtrlSetBkColor($p_Status, 0x3FFF00)

Sleep(3000)

guictrlsetbkcolor($p_status, 0xADFF2F)

$theshit = Ping($inputText, 3000)

if $theshit = 0 Then

adlibunregister("_checkostatus")

_CheckOFFstatus()

EndIf

EndFunc ;==>_CheckOstatus

Func _CheckOFFstatus()

GUICtrlSetState($p_Status, $GUI_SHOW)

GUICtrlSetData($p_Status, "Offline")

GUICtrlSetBkColor($p_Status, 0xFF0800)

EndFunc ;==>_CheckOFFstatus

Share this post


Link to post
Share on other sites
martin

Well I don't know whether that is bad luck or good luck :D You have found a bug (or at least I haven't come across this before). The function name in AdlibUnregister is case sensitive.

So that is all that is wrong with your script. I would suggest a small change though because you are not making the best use of the adlib feature. The function is called every period that you set and otherwise the rest of your script has the time to execute, so it is a shame to have delays in the adlib function. I would have one adlib function and use a global variable for count, something like this

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=e:\autoit\command kit\commander.kxf
$Commander = GUICreate("Commander v1.0", 264, 201, 318, 193)
$File = GUICtrlCreateMenu("&File")
$Exit = GUICtrlCreateMenuItem("Exit", $File)
$View = GUICtrlCreateMenu("&View")
$Sview = GUICtrlCreateMenuItem("Simple View", $View)
$Aview = GUICtrlCreateMenuItem("Advanced View", $View)
$Help = GUICtrlCreateMenu("&Help")
$Usage = GUICtrlCreateMenuItem("How To Use Commander", $Help)
$Aboutkit = GUICtrlCreateMenuItem("About Commander", $Help)
$Group1 = GUICtrlCreateGroup("", 8, 8, 244, 161)
$Instructions = GUICtrlCreateLabel("Enter Hostname or IP", 20, 30, 105, 17)
$InputData = GUICtrlCreateInput("", 16, 52, 113, 21)
$btn_Ping = GUICtrlCreateButton("Ping", 16, 80, 65, 20, BitOR($BS_DEFPUSHBUTTON, $BS_PUSHLIKE))
GUICtrlSetTip($btn_Ping, "This Pings Remote Host")
$btn_Look = GUICtrlCreateButton("Nslookup", 16, 108, 65, 20, $WS_GROUP)
GUICtrlSetTip($btn_Look, "Finds Host Name For Give IP/ IP For Given Host Name")
$btn_RDP = GUICtrlCreateButton("Remote Into Machine", 16, 136, 113, 25, $WS_GROUP)
GUICtrlSetTip($btn_RDP, "Remotely Connects To Host/IP Specified")
$Radio_SMS = GUICtrlCreateRadio("SMS", 144, 136, 49, 25)
$Radio_RDP = GUICtrlCreateRadio("RDP", 200, 136, 49, 25)
$Ping_Dash = GUICtrlCreateLabel("-", 96, 83, 9, 20)
GUICtrlSetFont($Ping_Dash, 10, 800, 0, "MS Sans Serif")
$Look_Dash = GUICtrlCreateLabel("-", 96, 110, 9, 20)
GUICtrlSetFont($Look_Dash, 10, 800, 0, "MS Sans Serif")
$Label4 = GUICtrlCreateLabel("", 112, 83, 4, 4, $SS_CENTER)
$p_Status = GUICtrlCreateLabel("", 120, 80, 80, 20, BitOR($SS_CENTER, $WS_BORDER))
GUICtrlSetFont($p_Status, 8, 800, 4, "MS Sans Serif")
$n_Status = GUICtrlCreateLabel("", 120, 108, 113, 20, BitOR($SS_CENTER, $WS_BORDER))
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
Dim $Commander_AccelTable[1][2] = [["^+{F21}", $Aboutkit]]
GUISetAccelerators ($Commander_AccelTable)
#EndRegion ### END Koda GUI section ###

Global $count = 0

While 1 ; keeps the gui opening
    $nMsg = GUIGetMsg() ; a listening event. stores what ever happens in $nmsg


    Switch $nMsg
        Case $GUI_EVENT_CLOSE ; if red x is clicked. close program
            Exit


        Case $btn_Ping ; if ping button is clicked perform the following.

            Global $inputText = GUICtrlRead($InputData) ; takes texted entered into the text box and stores it in variable.
            Global $ping_info = Ping($inputText, 3000) ; ping data and stores it in a variable. The return value returns numbers

            If @error = 0 And StringLen($inputText) < 1 Then ; if there is no error and there is no text entered, prompt
                GUICtrlSetData($InputData, "Enter IP or Hostname !")

            ElseIf $inputText = "Enter Ip or Hostname !" Then ; if there is nothing entered, prompt.
                GUICtrlSetData($InputData, "Enter IP or Hostname !")

                GUICtrlSetDefColor ($p_Status)
            ElseIf @error = 0 Then ;if there is no error, display online
                AdlibRegister ("_CheckOstatus", 2500)

                GUICtrlSetState($p_Status, $GUI_SHOW)
                
            EndIf
        Case $Radio_RDP ; If the radio_SMS or Radio_rdp is checked, then enable btn_RDP

            If $Radio_RDP Or $Radio_SMS = $GUI_Checked Then
                GUICtrlSetState($btn_RDP, $GUI_Enable)
            EndIf

        Case $Radio_SMS ; If the radio_SMS or Radio_rdp is checked, then enable btn_RDP

            If $Radio_RDP Or $Radio_SMS = $GUI_Checked Then
                GUICtrlSetState($btn_RDP, $GUI_Enable)

            EndIf
        Case $btn_RDP ; if the RDP button is clicked do the following below.

            $rdpstatcheck = GUICtrlRead($Radio_RDP) ; stores current status of button into a variable
            $smsstatcheck = GUICtrlRead($Radio_SMS) ; stores current status of button into a variable
            $inputText = GUICtrlRead($InputData) & "{space}" & "{SPACE}" ; what ever is entered into text box, tape space bar twice and store this in a variable.


            If $rdpstatcheck = 1 And StringLen($inputText) > 1 Then ; if rdp_radio is selected, do the following.
                Run("mstsc.exe") ; runs the program.
                Sleep(1000) ; waits 1 second.
                ControlSend("Remote Desktop Connection", "", "Edit1", $inputText) ; send the following info and so on.
                Sleep(800)
                ControlSend("Remote Desktop Connection", "", "Button5", "{ENTER}")
                Sleep(2000)
                If WinActive("Remote Desktop Connection") Then
                    ControlSend("Remote Desktop Connection", "", "Button1", "{Space}")
                    Sleep(500)
                    ControlSend("Remote Desktop Connection", "", "Button1", "{Right}")
                    ControlSend("Remote Desktop Connection", "", "Button1", "{Enter}")
                EndIf


            EndIf

    EndSwitch

WEnd


Func _CheckOstatus()
    $count = 1 - $count;toggle
    
    $theshit = Ping($inputText, 3000)
    If $theshit = 0 Then
        If GUICtrlRead($p_Status) <> "Offline" Then
            GUICtrlSetData($p_Status, "Offline")
            GUICtrlSetBkColor($p_Status, 0xFF0800)
        EndIf
    Else
        If $count Then
            GUICtrlSetData($p_Status, "Online")
            GUICtrlSetBkColor($p_Status, 0x3FFF00)
        Else
            GUICtrlSetBkColor($p_Status, 0xADFF2F)
        EndIf
    EndIf


EndFunc ;==>_CheckOstatus

BTW, the delays caused by Ping can be a problem for some scripts, and I have a link to udf in my signature which avoids this problem.

EDIT: I see more clearly what the problem with AdlibUnregister is now. The function name in unregister is case sensitively compared to the function name registered, whereas the function in AdlibRegister will work regardless of case.

Edited by martin

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
J03Z

Well I don't know whether that is bad luck or good luck :D You have found a bug (or at least I haven't come across this before). The function name in AdlibUnregister is case sensitive.

I was somehow mistaken I think, but I thought the script worked when I corrected the case. However I cvan't get AdlibUnregister to have any effect (production 3.3.5.0)

So that is all that is wrong with your script. I would suggest a small change though because you are not making the best use of the adlib feature. The function is called every period that you set and otherwise the rest of your script has the time to execute, so it is a shame to have delays in the adlib function. I would have one adlib function and use a global variable for count, something like this

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=e:\autoit\command kit\commander.kxf
$Commander = GUICreate("Commander v1.0", 264, 201, 318, 193)
$File = GUICtrlCreateMenu("&File")
$Exit = GUICtrlCreateMenuItem("Exit", $File)
$View = GUICtrlCreateMenu("&View")
$Sview = GUICtrlCreateMenuItem("Simple View", $View)
$Aview = GUICtrlCreateMenuItem("Advanced View", $View)
$Help = GUICtrlCreateMenu("&Help")
$Usage = GUICtrlCreateMenuItem("How To Use Commander", $Help)
$Aboutkit = GUICtrlCreateMenuItem("About Commander", $Help)
$Group1 = GUICtrlCreateGroup("", 8, 8, 244, 161)
$Instructions = GUICtrlCreateLabel("Enter Hostname or IP", 20, 30, 105, 17)
$InputData = GUICtrlCreateInput("", 16, 52, 113, 21)
$btn_Ping = GUICtrlCreateButton("Ping", 16, 80, 65, 20, BitOR($BS_DEFPUSHBUTTON, $BS_PUSHLIKE))
GUICtrlSetTip($btn_Ping, "This Pings Remote Host")
$btn_Look = GUICtrlCreateButton("Nslookup", 16, 108, 65, 20, $WS_GROUP)
GUICtrlSetTip($btn_Look, "Finds Host Name For Give IP/ IP For Given Host Name")
$btn_RDP = GUICtrlCreateButton("Remote Into Machine", 16, 136, 113, 25, $WS_GROUP)
GUICtrlSetTip($btn_RDP, "Remotely Connects To Host/IP Specified")
$Radio_SMS = GUICtrlCreateRadio("SMS", 144, 136, 49, 25)
$Radio_RDP = GUICtrlCreateRadio("RDP", 200, 136, 49, 25)
$Ping_Dash = GUICtrlCreateLabel("-", 96, 83, 9, 20)
GUICtrlSetFont($Ping_Dash, 10, 800, 0, "MS Sans Serif")
$Look_Dash = GUICtrlCreateLabel("-", 96, 110, 9, 20)
GUICtrlSetFont($Look_Dash, 10, 800, 0, "MS Sans Serif")
$Label4 = GUICtrlCreateLabel("", 112, 83, 4, 4, $SS_CENTER)
$p_Status = GUICtrlCreateLabel("", 120, 80, 80, 20, BitOR($SS_CENTER, $WS_BORDER))
GUICtrlSetFont($p_Status, 8, 800, 4, "MS Sans Serif")
$n_Status = GUICtrlCreateLabel("", 120, 108, 113, 20, BitOR($SS_CENTER, $WS_BORDER))
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
Dim $Commander_AccelTable[1][2] = [["^+{F21}", $Aboutkit]]
GUISetAccelerators ($Commander_AccelTable)
#EndRegion ### END Koda GUI section ###

Global $count = 0

While 1 ; keeps the gui opening
    $nMsg = GUIGetMsg() ; a listening event. stores what ever happens in $nmsg


    Switch $nMsg
        Case $GUI_EVENT_CLOSE ; if red x is clicked. close program
            Exit


        Case $btn_Ping ; if ping button is clicked perform the following.

            Global $inputText = GUICtrlRead($InputData) ; takes texted entered into the text box and stores it in variable.
            Global $ping_info = Ping($inputText, 3000) ; ping data and stores it in a variable. The return value returns numbers

            If @error = 0 And StringLen($inputText) < 1 Then ; if there is no error and there is no text entered, prompt
                GUICtrlSetData($InputData, "Enter IP or Hostname !")

            ElseIf $inputText = "Enter Ip or Hostname !" Then ; if there is nothing entered, prompt.
                GUICtrlSetData($InputData, "Enter IP or Hostname !")

                GUICtrlSetDefColor ($p_Status)
            ElseIf @error = 0 Then ;if there is no error, display online
                AdlibRegister ("_CheckOstatus", 2500)

                GUICtrlSetState($p_Status, $GUI_SHOW)
                
            EndIf
        Case $Radio_RDP ; If the radio_SMS or Radio_rdp is checked, then enable btn_RDP

            If $Radio_RDP Or $Radio_SMS = $GUI_Checked Then
                GUICtrlSetState($btn_RDP, $GUI_Enable)
            EndIf

        Case $Radio_SMS ; If the radio_SMS or Radio_rdp is checked, then enable btn_RDP

            If $Radio_RDP Or $Radio_SMS = $GUI_Checked Then
                GUICtrlSetState($btn_RDP, $GUI_Enable)

            EndIf
        Case $btn_RDP ; if the RDP button is clicked do the following below.

            $rdpstatcheck = GUICtrlRead($Radio_RDP) ; stores current status of button into a variable
            $smsstatcheck = GUICtrlRead($Radio_SMS) ; stores current status of button into a variable
            $inputText = GUICtrlRead($InputData) & "{space}" & "{SPACE}" ; what ever is entered into text box, tape space bar twice and store this in a variable.


            If $rdpstatcheck = 1 And StringLen($inputText) > 1 Then ; if rdp_radio is selected, do the following.
                Run("mstsc.exe") ; runs the program.
                Sleep(1000) ; waits 1 second.
                ControlSend("Remote Desktop Connection", "", "Edit1", $inputText) ; send the following info and so on.
                Sleep(800)
                ControlSend("Remote Desktop Connection", "", "Button5", "{ENTER}")
                Sleep(2000)
                If WinActive("Remote Desktop Connection") Then
                    ControlSend("Remote Desktop Connection", "", "Button1", "{Space}")
                    Sleep(500)
                    ControlSend("Remote Desktop Connection", "", "Button1", "{Right}")
                    ControlSend("Remote Desktop Connection", "", "Button1", "{Enter}")
                EndIf


            EndIf

    EndSwitch

WEnd


Func _CheckOstatus()
    $count = 1 - $count;toggle
    
    $theshit = Ping($inputText, 3000)
    If $theshit = 0 Then
        If GUICtrlRead($p_Status) <> "Offline" Then
            GUICtrlSetData($p_Status, "Offline")
            GUICtrlSetBkColor($p_Status, 0xFF0800)
        EndIf
    Else
        If $count Then
            GUICtrlSetData($p_Status, "Online")
            GUICtrlSetBkColor($p_Status, 0x3FFF00)
        Else
            GUICtrlSetBkColor($p_Status, 0xADFF2F)
        EndIf
    EndIf


EndFunc ;==>_CheckOstatus

BTW, the delays caused by Ping can be a problem for some scripts, and I have a link to udf in my signature which avoids this problem.

EDIT: I see more clearly what the problem with AdlibUnregister is now. The function name in unregister is case sensitively compared to the function name registered, whereas the function in AdlibRegister will work regardless of case.

Thanks for all your help Martin, This was a big boost for me. When I get the program complete I will be sure to let you know.

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  

×