Jump to content

help to end the loop


 Share

Recommended Posts

hi to all. can please somebody check my script. i have a form with button of network info and system info. i am able to query all the data to be displayed on a listview item. my problem is, after the data appeared on the listview, i cannot exit the listview and even close the form. i don't know on which part of the script it is looping.

thanks for your help.

code:

#include <GUIConstants.au3>

#include <GUIConstantsEx.au3>

#include <WindowsConstants.au3>

#include <ButtonConstants.au3>

#include <EditConstants.au3>

#include <GuiEdit.au3>

#include <Array.au3>

#include <ListviewConstants.au3>

#Include <GuiListView.au3>

#include <GuiEdit.au3>

#include <GuiMenu.au3>

Opt("GUIOnEventMode", 2)

Global $aSoft[1]

Global $objItem, $listview, $Data, $gui

$Form1 = GUICreate("Form1", 433, 351, 214, 372)

GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")

$Label_Computer = GUICtrlCreateLabel("Computer Name / IP Address :", 24, 24, 176, 17)

GUICtrlSetFont($Label_Computer, 8, 800, 0, "MS Sans Serif")

$Input_Computer = GUICtrlCreateInput("", 203, 19, 203, 21)

$Group1 = GUICtrlCreateGroup("Tasks", 24, 56, 385, 177)

GUICtrlSetFont($Group1, 8, 800, 0, "MS Sans Serif")

$Button_Ping = GUICtrlCreateButton("Ping Computer", 48, 88, 105, 25, 0)

GUICtrlSetOnEvent($Button_Ping, "Button_PingClick")

$Button_NetworkInfo = GUICtrlCreateButton("Network Information", 48, 137, 105, 25, 0)

GUICtrlSetOnEvent($Button_NetworkInfo, "Button_NetworkInfoClick")

$Button_SystemInfo = GUICtrlCreateButton("System Information", 48, 183, 105, 26, 0)

GUICtrlSetOnEvent($Button_SystemInfo, "Button_SystemInfoClick")

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

$Status = GUICtrlCreateEdit("", 24, 256, 385, 70, BitOR($ES_READONLY, $WS_VSCROLL, $WS_HSCROLL, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL))

GUISetState(@SW_SHOW)

While 1

Switch $Form1

Case $Button_Ping

Button_PingClick()

Case $Button_NetworkInfo

Button_NetworkInfoClick()

Case $Button_SystemInfo

Button_SystemInfoClick()

EndSwitch

WEnd

Func Button_PingClick()

Local $check_computer = GUICtrlRead($Input_Computer), $PC = "\\" & $check_computer

$ping = Ping(GUICtrlRead($Input_Computer))

If $ping Then

GUISetState(@SW_DISABLE, $Form1)

GUICtrlSetData($Status, "Reply from computer. It is ONLINE.")

$msg = MsgBox(64, $check_computer, "Computer is ONLINE!")

If $msg = 1 Then

GUISetState(@SW_ENABLE, $Form1)

EndIf

Else

GUICtrlSetData($Status, "Computer could not be contacted.")

MsgBox(16, "Error", "Computer could not be contacted.")

EndIf

EndFunc

Func Button_NetworkInfoClick()

Dim $NICInfo[1][5], $i = 1

$strComputer = (GUICtrlRead($Input_Computer))

$ping = Ping($strComputer)

If $ping Then

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")

$colNicConfigs = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

If IsObj($colNicConfigs) Then

For $objItem In $colNicConfigs

ReDim $NICInfo[uBound($NICInfo) + 1][5]

$NICInfo[$i][0] = $objItem.DNSHostName

$strIPAddress = $objItem.IPAddress(0)

$NICInfo[$i][1] = $strIPAddress

$NICInfo[$i][2] = $objItem.MACAddress

$NICInfo[$i][3] = $objItem.DHCPServer

$NICInfo[$i][4] = $objItem.Description

$i += 1

Next

$gui = GUICreate("Network Information", 520, 125, 100, 100, -1, $WS_EX_ACCEPTFILES)

$listview = GUICtrlCreateListView("", 10, 10, 495, 100)

_GUICtrlListView_AddColumn ($listview, "Computer Name", 100)

_GUICtrlListView_AddColumn ($listview, "IP Address", 100)

_GUICtrlListView_AddColumn ($listview, "MACAddress", 100)

_GUICtrlListView_AddColumn ($listview, "DHCP Server", 100)

_GUICtrlListView_AddColumn ($listview, "NIC Description", 180)

GUICtrlSetState(-1, $GUI_DROPACCEPTED)

GUISetState()

For $i = 1 To UBound($NICInfo) - 1

$tmp = $NICInfo[$i][0] & "|" & $NICInfo[$i][1] & "|" & $NICInfo[$i][2] & "|" & $NICInfo[$i][3] & "|" & $NICInfo[$i][4]

GUICtrlCreateListViewItem($tmp, $listview)

GUICtrlSetData($Status, "Computer Name: " & @TAB & $objItem.DNSHostName & @CRLF _

& "MAC Address: " & @TAB & $objItem.MACAddress & @CRLF _

& "IP Address: " & @TAB & $strIPAddress & @CRLF _

& "DHCP Server: " & @TAB & $objItem.DHCPServer & @CRLF _

& "NIC Description: " & @TAB & $objItem.Description)

Next

While 1

Switch $gui

Case $GUI_EVENT_CLOSE

Exit

EndSwitch

Wend

EndIf

Else

MsgBox(16, "Error", "Could not locate computer!")

EndIf

EndFunc

Func Button_SystemInfoClick()

Dim $NICInfo[1][9], $i = 1

$strComputer = (GUICtrlRead($Input_Computer))

$ping = Ping($strComputer)

If $ping Then

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")

$colNicConfigs = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")

If IsObj($colNicConfigs) Then

For $objItem In $colNicConfigs

ReDim $NICInfo[uBound($NICInfo) + 1][9]

$NICInfo[$i][0] = $objItem.CSName

$NICInfo[$i][1] = $objItem.Caption

$NICInfo[$i][2] = $objItem.CSDVersion

$NICInfo[$i][3] = $objItem.Manufacturer

$NICInfo[$i][4] = $objItem.SystemDirectory

$NICInfo[$i][5] = $objItem.RegisteredUser

$NICInfo[$i][6] = $objItem.SerialNumber

$NICInfo[$i][7] = WMIDateStringToDate($objItem.InstallDate)

$NICInfo[$i][8] = WMIDateStringToDate($objItem.LastBootUpTime)

$i += 1

Next

$NICInfo[0][0] = UBound($NICInfo) - 1

If $NICInfo[0][0] < 1 Then

SetError(1, 1, 0)

EndIf

Else

SetError(1, 2, 0)

EndIf

GUICreate("Operating System Information", 520, 125, 100, 100, -1, $WS_EX_ACCEPTFILES)

$listview = GUICtrlCreateListView("", 10, 10, 495, 100)

_GUICtrlListView_AddColumn ($listview, "Computer Name", 120)

_GUICtrlListView_AddColumn ($listview, "Operating System", 180)

_GUICtrlListView_AddColumn ($listview, "Service Pack", 100)

_GUICtrlListView_AddColumn ($listview, "Vendor", 120)

_GUICtrlListView_AddColumn ($listview, "Windows System Directory", 150)

_GUICtrlListView_AddColumn ($listview, "Registered To", 100)

_GUICtrlListView_AddColumn ($listview, "Serial Number", 150)

_GUICtrlListView_AddColumn ($listview, "Installation Date", 110)

_GUICtrlListView_AddColumn ($listview, "Last BootUp Time", 110)

GUICtrlSetState(-1, $GUI_DROPACCEPTED)

GUISetState()

For $i = 1 To UBound($NICInfo) - 1

$tmp = $NICInfo[$i][0] & "|" & $NICInfo[$i][1] & "|" & $NICInfo[$i][2] & "|" & $NICInfo[$i][3] & "|" & $NICInfo[$i][4] & "|" & $NICInfo[$i][5] & "|" & $NICInfo[$i][6] _

& "|" & $NICInfo[$i][7] & "|" & $NICInfo[$i][8]

GUICtrlCreateListViewItem($tmp, $listview)

Next

While 1

If GUIGetMsg() = -3 Then Exit

WEnd

Else

MsgBox(16, "Error", "Could not locate computer!")

EndIf

EndFunc

Func WMIDateStringToDate($dtmDate)

Return (StringMid($dtmDate, 5, 2) & "/" & _

StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _

& " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))

EndFunc

Func Form1Close()

Exit

EndFunc

Link to comment
Share on other sites

I haven't studied it all but a good start would be to change

Switch $Form1

to

Switch GuiGetMsg()

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.
Link to comment
Share on other sites

hi martin. have you studied the code? can someone help me how to exit when the listview item is displayed. it seems that the script stucked and trying to continue the loop.

i appreciate much if somebody can give me clue why it doesn't want to exit.

I can't get it to run without errors, but you have this which looks like it should be removed from Buton_NetworkInfoClick()

While 1
 Switch $gui;<--same error here, it should be GuiGetMsg() but the whole While/Wend is not wanted
   Case $GUI_EVENT_CLOSE
    Exit
 EndSwitch
Wend

If you use SciTE then use Ctrl t to tidy your code, and when you post enclose you script in code tags [ c o d e ] before and [ / c o d e ] after but without the spaces which I added so that you can read them. Both of these changes make you script easier to read.

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.
Link to comment
Share on other sites

i removed the unwanted while/wend on the script. the listview gui exits and the whole gui exits as well. so i can i handle to exit only the listview gui? If i put If/Then statement to the after Next, the script will hang and cold not be close.

I don't know what your lates script looks like so I can't make any suggestions. :)
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.
Link to comment
Share on other sites

here is the code looks like:

#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GuiEdit.au3>
#include <Array.au3>
#include <ListviewConstants.au3>
#Include <GuiListView.au3>
#include <GuiEdit.au3>
#include <GuiMenu.au3>

Opt("GUIOnEventMode", 1)


Global $aSoft[1]
Global $objItem, $listview, $Data, $gui

$Form1 = GUICreate("Form1", 433, 351, 214, 372)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Label_Computer = GUICtrlCreateLabel("Computer Name / IP Address :", 24, 24, 176, 17)
GUICtrlSetFont($Label_Computer, 8, 800, 0, "MS Sans Serif")
$Input_Computer = GUICtrlCreateInput("", 203, 19, 203, 21)
$Group1 = GUICtrlCreateGroup("Tasks", 24, 56, 385, 177)
GUICtrlSetFont($Group1, 8, 800, 0, "MS Sans Serif")
$Button_Ping = GUICtrlCreateButton("Ping Computer", 48, 88, 105, 25, 0)
GUICtrlSetOnEvent($Button_Ping, "Button_PingClick")
$Button_NetworkInfo = GUICtrlCreateButton("Network Information", 48, 137, 105, 25, 0)
GUICtrlSetOnEvent($Button_NetworkInfo, "Button_NetworkInfoClick")
$Button_SystemInfo = GUICtrlCreateButton("System Information", 48, 183, 105, 26, 0)
GUICtrlSetOnEvent($Button_SystemInfo, "Button_SystemInfoClick")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Status = GUICtrlCreateEdit("", 24, 256, 385, 70, BitOR($ES_READONLY, $WS_VSCROLL, $WS_HSCROLL, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
GUISetState(@SW_SHOW)


While 1 
    Switch GUIGetMsg()
        Case $Button_Ping
            Button_PingClick()
        Case $Button_NetworkInfo
            Button_NetworkInfoClick()
        Case $Button_SystemInfo
            Button_SystemInfoClick()
        Case $GUI_EVENT_CLOSE
            Form1Close()        
    EndSwitch
WEnd


Func Button_PingClick()
    Local $check_computer = GUICtrlRead($Input_Computer), $PC = "\\" & $check_computer
    $ping = Ping(GUICtrlRead($Input_Computer))
    If $ping Then       
        GUISetState(@SW_DISABLE, $Form1)
        GUICtrlSetData($Status, "Reply from computer. It is ONLINE.")
        $msg = MsgBox(64, $check_computer, "Computer is ONLINE!")
        If $msg = 1 Then
            GUISetState(@SW_ENABLE, $Form1)
        EndIf
    Else        
        GUICtrlSetData($Status, "Computer could not be contacted.")
        MsgBox(16, "Error", "Computer could not be contacted.")
    EndIf
EndFunc


Func Button_NetworkInfoClick()  

Dim $NICInfo[1][5], $i = 1
$strComputer = (GUICtrlRead($Input_Computer))       
$ping = Ping($strComputer)
If $ping Then
        $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
        $colNicConfigs = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

    If IsObj($colNicConfigs) Then
        For $objItem In $colNicConfigs
            ReDim $NICInfo[UBound($NICInfo) + 1][5] 
                $NICInfo[$i][0] = $objItem.DNSHostName
                $strIPAddress = $objItem.IPAddress(0)   
                $NICInfo[$i][1] = $strIPAddress
                $NICInfo[$i][2] = $objItem.MACAddress
                $NICInfo[$i][3] = $objItem.DHCPServer       
                $NICInfo[$i][4] = $objItem.Description      
                $i += 1
        Next
                $gui = GUICreate("Network Information", 520, 125, 100, 100, -1, $WS_EX_ACCEPTFILES)                             
                $listview = GUICtrlCreateListView("", 10, 10, 495, 100)
                _GUICtrlListView_AddColumn ($listview, "Computer Name", 100)
                _GUICtrlListView_AddColumn ($listview, "IP Address", 100)
                _GUICtrlListView_AddColumn ($listview, "MACAddress", 100)
                _GUICtrlListView_AddColumn ($listview, "DHCP Server", 100)
                _GUICtrlListView_AddColumn ($listview, "NIC Description", 180)
                GUICtrlSetState(-1, $GUI_DROPACCEPTED)
                GUISetState()               

                For $i = 1 To UBound($NICInfo) - 1
                    $tmp = $NICInfo[$i][0] & "|" & $NICInfo[$i][1] & "|" & $NICInfo[$i][2] & "|" & $NICInfo[$i][3] & "|" & $NICInfo[$i][4]                  
                    GUICtrlCreateListViewItem($tmp, $listview)                      
                    GUICtrlSetData($Status, "--------------- Network Information Details ---------------" & @CRLF _                 
                    & "Computer Name: " & @TAB & $objItem.DNSHostName & @CRLF _
                    & "MAC Address: " & @TAB & $objItem.MACAddress & @CRLF _
                    & "IP Address: " & @TAB & $strIPAddress & @CRLF _
                    & "DHCP Server: " & @TAB & $objItem.DHCPServer & @CRLF _
                    & "NIC Description: " & @TAB & $objItem.Description)
                                                                                
                Next    
                            GUISwitch($gui)
                            $gui = GUIGetMsg()
                            If $gui = $GUI_EVENT_CLOSE Then 
                                    GUIDelete($gui)
                            EndIf
    EndIf
Else
    MsgBox(16, "Error", "Could not locate computer!")
EndIf

EndFunc



Func Button_SystemInfoClick()
    Dim $NICInfo[1][9], $i = 1
    $strComputer = (GUICtrlRead($Input_Computer))
    $ping = Ping($strComputer)
    If $ping Then
        $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
        $colNicConfigs = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")

        If IsObj($colNicConfigs) Then
            For $objItem In $colNicConfigs
            ReDim $NICInfo[UBound($NICInfo) + 1][9] 
                $NICInfo[$i][0] = $objItem.CSName
                $NICInfo[$i][1] = $objItem.Caption  
                $NICInfo[$i][2] = $objItem.CSDVersion   
                $NICInfo[$i][3] = $objItem.Manufacturer
                $NICInfo[$i][4] = $objItem.SystemDirectory
                $NICInfo[$i][5] = $objItem.RegisteredUser
                $NICInfo[$i][6] = $objItem.SerialNumber
                $NICInfo[$i][7] = WMIDateStringToDate($objItem.InstallDate)
                $NICInfo[$i][8] = WMIDateStringToDate($objItem.LastBootUpTime)  
                $i += 1
            Next
            ;$NICInfo[0][0] = UBound($NICInfo) - 1
            ;If $NICInfo[0][0] < 1 Then
                ;SetError(1, 1, 0)
            ;EndIf
            ;Else
                ;SetError(1, 2, 0)
        EndIf
        GUICreate("Operating System Information", 520, 125, 100, 100, -1, $WS_EX_ACCEPTFILES)
        $listview = GUICtrlCreateListView("", 10, 10, 495, 100)
        _GUICtrlListView_AddColumn ($listview, "Computer Name", 120)
        _GUICtrlListView_AddColumn ($listview, "Operating System", 180)
        _GUICtrlListView_AddColumn ($listview, "Service Pack", 100)
        _GUICtrlListView_AddColumn ($listview, "Vendor", 120)
        _GUICtrlListView_AddColumn ($listview, "Windows System Directory", 150)
        _GUICtrlListView_AddColumn ($listview, "Registered To", 100)
        _GUICtrlListView_AddColumn ($listview, "Serial Number", 150)
        _GUICtrlListView_AddColumn ($listview, "Installation Date", 110)
        _GUICtrlListView_AddColumn ($listview, "Last BootUp Time", 110)
        GUICtrlSetState(-1, $GUI_DROPACCEPTED)
        GUISetState()

        For $i = 1 To UBound($NICInfo) - 1
            $tmp = $NICInfo[$i][0] & "|" & $NICInfo[$i][1] & "|" & $NICInfo[$i][2] & "|" & $NICInfo[$i][3] & "|" & $NICInfo[$i][4] & "|" & $NICInfo[$i][5] & "|" & $NICInfo[$i][6] _
                    & "|" & $NICInfo[$i][7] & "|" & $NICInfo[$i][8]
            GUICtrlCreateListViewItem($tmp, $listview)
        Next        
    Else
        MsgBox(16, "Error", "Could not locate computer!")
    EndIf
EndFunc



Func WMIDateStringToDate($dtmDate)
    Return (StringMid($dtmDate, 5, 2) & "/" & _
    StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc


Func Form1Close()
    Exit
EndFunc
Link to comment
Share on other sites

here is the code looks like:

; ...

Opt("GUIOnEventMode", 1)

; ...

While 1 
    Switch GUIGetMsg()
    ; ...
    EndSwitch
WEnd
You have to choose between a message loop OR event mode. You can't do both, pick one and drop the other.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...