meoit

Check Network and start an URL

3 posts in this topic

#1 ·  Posted (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:

Opt('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 by meoit

Share this post


Link to post
Share on other sites



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

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

Thanks to @Melba23 very much!!

It is now working fine! :)

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