meoit Posted October 24, 2016 Share Posted October 24, 2016 (edited) Hi all! Sorry for my bad English. I am try writing an GUI to check the Network and start an URL. But I can not close Main Form and it seems that my CPU has high activity. This is my code: expandcollapse popupOpt('MustDeclareVars', 1) Opt('GUICloseOnEsc', 1) Opt('TrayIconHide', 1) Opt('TrayMenuMode', 1) ; Global Const $Txt_Not_Connect = 'Lost network connection.', $Txt_Connect = 'Have network connection.' Global $Bool_Connected ; #include <Constants.au3> #include <GUIConstants.au3> ; Global $MAIN_FORM = GUICreate('Have Network and Start an URL', 305, 60, -1, -1) Global $BT_START = GUICtrlCreateButton('Start AutoIT', 5, 10, 105, 25) Global $BT_ABOUT = GUICtrlCreateButton('Start Google', 195, 10, 105, 25) Global $LB_STATUS = GUICtrlCreateLabel('Network Status', 5, 40, 300, -1) ; GUISetState(@SW_SHOW) Global $nMsg = GUIGetMsg() ; While 1 Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $BT_START F_BT_START_AUTOIT() Case $BT_ABOUT F_BT_START_GOOGLE() EndSwitch F_NETWORK_CONNECTED() WEnd ; Func F_SET_DATA_LABEL() If $Bool_Connected Then GUICtrlSetColor($LB_STATUS, 0x008B8B) ;DarkCyan GUICtrlSetData($LB_STATUS, $Txt_Connect & ' I am READY.') Else GUICtrlSetColor($LB_STATUS, 0xDC143C) ;Red GUICtrlSetData($LB_STATUS, $Txt_Not_Connect & ' I am NOT READY.') EndIf EndFunc ; Func F_BT_START_AUTOIT() If $Bool_Connected Then F_START_URL('https://www.autoitscript.com/forum/') Else MsgBox(64 + 262144, 'No connection :(', $Txt_Not_Connect) EndIf EndFunc ; Func F_BT_START_GOOGLE() If $Bool_Connected Then F_START_URL('https://www.google.com/') Else MsgBox(64 + 262144, 'No connection :(', $Txt_Not_Connect) EndIf EndFunc ; Func F_NETWORK_CONNECTED() Local $net_test = F_CHECK_CNN() If $net_test = 1 Then $Bool_Connected = true Else $Bool_Connected = false EndIf Sleep(1000) F_SET_DATA_LABEL() EndFunc ; Func F_CHECK_CNN() ; Returns 1 = ON or 0 = OFF Local $is_Return = DllCall('wininet.dll', 'int', 'InternetGetConnectedState', 'int', 0, 'int', 0) If (@error) Or ($is_Return[0] = 0) Then Return SetError(1, 0, 0) Return 1 EndFunc ;==>F_CHECK_CNN ; Func F_START_URL($s_StartPath) Local $s_StartStr If @OSType = 'WIN32_NT' Then $s_StartStr = @ComSpec & ' /c start "" ' Else $s_StartStr = @ComSpec & ' /c start ' EndIf Run($s_StartStr & $s_StartPath, '', @SW_HIDE) EndFunc I want to loop the test network connectivity. When the user running the Main Form, Network Status Label still display correct the network connection status. How I can fix my problems?. Thanks for support. Edited October 24, 2016 by meoit Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 24, 2016 Moderators Share Posted October 24, 2016 meoit, You need to have the GUIGetMsg line inside the loop - having outside explains why you cannot close the GUI as the $GUI_EVENT_CLOSE event is never detected. And I would not check the network status on every pass - try once a second like this: Global $nMsg ; Global $MAIN_FORM = GUICreate('Have Network and Start an URL', 305, 60, -1, -1) Global $BT_START = GUICtrlCreateButton('Start AutoIT', 5, 10, 105, 25) Global $BT_ABOUT = GUICtrlCreateButton('Start Google', 195, 10, 105, 25) Global $LB_STATUS = GUICtrlCreateLabel('Network Status', 5, 40, 300, -1) ; GUISetState(@SW_SHOW) ; Global $nBegin = TimerInit() ; Set a timestamp ; While 1 $nMsg = GUIGetMsg() ; Inside the loop Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $BT_START F_BT_START_AUTOIT() Case $BT_ABOUT F_BT_START_GOOGLE() EndSwitch If TimerDiff($nBegin) > 1000 Then ; If a second since the time stamp... F_NETWORK_CONNECTED() ; ...test the network connection... $nBegin = TimerInit() ; ...and reset the timestamp EndIf WEnd M23 meoit 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
meoit Posted October 24, 2016 Author Share Posted October 24, 2016 Thanks to @Melba23 very much!! It is now working fine! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now