Sign in to follow this  
Followers 0
d4rk

func inside loop

16 posts in this topic

2, i have a program to check the state of internet connection, but it doesn't work, here is the code

Global $ping
while 1
if $ping = Ping("www.google.com.vn") < 0 Then; right away when the connection fail
recheck();call this func to check again after 5 secs
Else;if not, keep checking
    ContinueLoop
EndIf

Func recheck()
sleep(5000)
    if $ping = Ping("www.google.com.vn") < 0 Then; the connection is really down, exit this loop
    ExitLoop
Else
    ContinueLoop;the connection is ok now, don't need to inform the user about this, keep looping from the beginning
    
EndIf

EndFunc
WEnd

MsgBox(64,"","Connection Fail For 5 Secs")

when i run the code, the error message tell that i can't use continueloop out side a loop, and i think this because i put it in a Func

thx for help


[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Share this post


Link to post
Share on other sites



That's just plain wrong, here's fixed code:

While 1
    If Ping("www.google.com.vn") = 0 Then; right away when the connection fail
        If recheck() Then ExitLoop
    EndIf

    Sleep(100)
WEnd

MsgBox(64, "", "Connection Fail For 5 Secs")


Func recheck()
    Sleep(5000)
    If Ping("www.google.com.vn") = 0 Then; the connection is really down, exit this loop
        Return 1
    Else
        Return 0
    EndIf
EndFunc   ;==>recheck

:D


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

EDIT: HA I like monoceres answer of "That's just plain wrong" better

You know the answer to this... just because you have a function defined inside a loop doesn't mean it is part of the loop itself. Instead of exitloop/continue loops, have the func return an error if you can't ping, or return something else if it is succesful. Then check that error in an if statement inside your loop.

Edited by someone

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Another solution

Global $ping
while 1
If Not _Connected() THen ExitLoop
WEnd



MsgBox(64,"","Connection Fail For 5 Secs")

Func _Connected()
If Ping("www.google.com.vn") = 0 Then
   sleep(5000)
   if Ping("www.google.com.vn") = 0 Then
     Return 0 ; the connection is really down
   Else
     Return 1 ;the connection is ok now, don't need to inform the user about this, keep looping from the beginning
   EndIf
Else
   Return 1 ; Conn OK
EndIf
EndFunc

//Edit: Too slow :D

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

Super compressed code:

while 1
    If Ping("www.google.com.vn")=0 Or Sleep(5000) And Ping("www.google.com.vn")=0 Then ExitLoop MsgBox(16,"No connection","Lost conection")
WEnd

I just wanted to brag :D


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Ok, now you guys have to give me the nobel price or something, reduced code to two lines!

Used Lazy Evaluation to achieve it :D

While (Ping("www.google.com.vn") > 0 Or Sleep(5000) And Ping("www.google.com.vn") > 0) Or (MsgBox(16, "No connection", "Lost conection") = 0)
WEnd

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

thanks all


[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Share this post


Link to post
Share on other sites

Imho, Ping is not so reliable solution to check internet connection, i use this:

Func IsConnected($sPingHost="google.com", $iPingTimeOut=1000)
    Local $iPing_Results = Ping($sPingHost, $iPingTimeOut)
    If $iPing_Results > 0 Then Return $iPing_Results
    
    Local $hWinInetDll = DllOpen("wininet.dll")
    If $hWinInetDll = -1 Then Return SetError(1, 0, -1)
    
    Local $aRet = DllCall($hWinInetDll, "int", "InternetGetConnectedState", "int", 0, "int", 0)
    DllClose($hWinInetDll)
    
    If IsArray($aRet) Then
        $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf
    
    If $aRet And $iPing_Results <= 0 Then Return 0
    
    Return $aRet
EndFunc

If the Ping fails, we check connection using API calls.


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

Imho, Ping is not so reliable solution to check internet connection, i use this:

Func IsConnected($sPingHost="google.com", $iPingTimeOut=1000)
    Local $iPing_Results = Ping($sPingHost, $iPingTimeOut)
    If $iPing_Results > 0 Then Return $iPing_Results
    
    Local $hWinInetDll = DllOpen("wininet.dll")
    If $hWinInetDll = -1 Then Return SetError(1, 0, -1)
    
    Local $aRet = DllCall($hWinInetDll, "int", "InternetGetConnectedState", "int", 0, "int", 0)
    DllClose($hWinInetDll)
    
    If IsArray($aRet) Then
        $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf
    
    If $aRet And $iPing_Results <= 0 Then Return 0
    
    Return $aRet
EndFunc

If the Ping fails, we check connection using API calls.

Hi! Sorry, but your example wrong and don`t work correctly. I found a perfect example :D

Share this post


Link to post
Share on other sites

I think this be a correct:

#include <Array.au3>

Global Const $INTERNET_CONNECTION_MODEM = 0x1
Global Const $INTERNET_CONNECTION_LAN = 0x2
Global Const $INTERNET_CONNECTION_PROXY = 0x4
Global Const $INTERNET_CONNECTION_MODEM_BUSY = 0x8
Global Const $INTERNET_RAS_INSTALLED = 0x10
Global Const $INTERNET_CONNECTION_OFFLINE = 0x20
Global Const $INTERNET_CONNECTION_CONFIGURED = 0x40

Dim $State, $val

$InetStruct = DllStructCreate("int")

DllCall("wininet.dll", "int", "InternetGetConnectedState", "ptr", DllStructGetPtr($InetStruct), "dword", 0)

$val = DllStructGetData($InetStruct, 1)

If BitAND($val, $INTERNET_CONNECTION_MODEM) Then $State &= "Modem connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_LAN) Then $State &= "LAN connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_PROXY) Then $State &= "Proxy connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_MODEM_BUSY) Then $State &= "Modem bussy" & @LF
If BitAND($val, $INTERNET_RAS_INSTALLED) Then $State &= "RAS installed" & @LF
If BitAND($val, $INTERNET_CONNECTION_OFFLINE) Then $State &= "Offline connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_CONFIGURED) Then $State &= "Connection configured"

MsgBox(0, "Connection", $State)

MSDN

Share this post


Link to post
Share on other sites

I think this be a correct:

#include <Array.au3>

Global Const $INTERNET_CONNECTION_MODEM = 0x1
Global Const $INTERNET_CONNECTION_LAN = 0x2
Global Const $INTERNET_CONNECTION_PROXY = 0x4
Global Const $INTERNET_CONNECTION_MODEM_BUSY = 0x8
Global Const $INTERNET_RAS_INSTALLED = 0x10
Global Const $INTERNET_CONNECTION_OFFLINE = 0x20
Global Const $INTERNET_CONNECTION_CONFIGURED = 0x40

Dim $State, $val

$InetStruct = DllStructCreate("int")

DllCall("wininet.dll", "int", "InternetGetConnectedState", "ptr", DllStructGetPtr($InetStruct), "dword", 0)

$val = DllStructGetData($InetStruct, 1)

If BitAND($val, $INTERNET_CONNECTION_MODEM) Then $State &= "Modem connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_LAN) Then $State &= "LAN connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_PROXY) Then $State &= "Proxy connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_MODEM_BUSY) Then $State &= "Modem bussy" & @LF
If BitAND($val, $INTERNET_RAS_INSTALLED) Then $State &= "RAS installed" & @LF
If BitAND($val, $INTERNET_CONNECTION_OFFLINE) Then $State &= "Offline connection" & @LF
If BitAND($val, $INTERNET_CONNECTION_CONFIGURED) Then $State &= "Connection configured"

MsgBox(0, "Connection", $State)

MSDN

thanks ... i'm surprise and have a question about DLL, exactly "wininet.dll", how do we know that there was a "InternetGetConnectedState" inside it and ... other, where can i learn about Dll ?

[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Hi! Sorry, but your example wrong and don`t work correctly. I found a perfect example :D

Hm, what exactly not working? you say "don't work", "wrong", you should show some arguments :) - I tested my (it's not fully mine actualy, it's from amel27 mostly :D ) example on few computers, work perfectly :D

And you should know, "InternetGetConnectedState" will return that computer is connected even if it isn't (well, not fully connected), but most of programs can't do anything related to network, in that case you should check Ping as well.

Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

Hm, what exactly not working? you say "don't work", "wrong", you should show some arguments :) - I tested my (it's not fully mine actualy, it's from amel27 mostly :D ) example on few computers, work perfectly :D

And you should know, "InternetGetConnectedState" will return that computer is connected even if it isn't (well, not fully connected), but most of programs can't do anything related to network, in that case you should check Ping as well.

I tested your code on few computers, but your code always returned a True value regardless of Internet connection state (connected, not connected). My computers have a LAN adapters and PCI-modems (Dial-UP connection). Example which i have found work fine! :D

Share this post


Link to post
Share on other sites

Example which i have found work fine!

Can you please tell me how in this example to check if there is a connection or not, i mean what should returns when there is connection, and what returns when isn't? what strings?

I am trying to make a function that will return True for connected, False for not.


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

MsCreatoR

Hi! With all respect for you! Below example works fine for me, because i have a Dial-UP connection:

Global Const $INTERNET_CONNECTION_MODEM = 0x1
Global Const $INTERNET_CONNECTION_LAN = 0x2
Global Const $INTERNET_CONNECTION_PROXY = 0x4
Global Const $INTERNET_CONNECTION_MODEM_BUSY = 0x8
Global Const $INTERNET_RAS_INSTALLED = 0x10
Global Const $INTERNET_CONNECTION_OFFLINE = 0x20
Global Const $INTERNET_CONNECTION_CONFIGURED = 0x40

$InetStruct = DllStructCreate("int")

DllCall("wininet.dll", "int", "InternetGetConnectedState", "ptr", DllStructGetPtr($InetStruct), "dword", 0)

$val = DllStructGetData($InetStruct, 1)

If BitAND($val, $INTERNET_CONNECTION_MODEM) Then
    MsgBox(64, "Connect", "Internet connected")
Else
    MsgBox(48, "Connect", "Internet not connected")
EndIf

Sorry, but i don`t know how this code be work on computers with ADSL connection. I don`t have a possibility to check this. :D

Edited by rasim

Share this post


Link to post
Share on other sites

Below example works fine for me

For me to, i have an ADSL modem :D

Lets continue in the PM, here it is offtopic already.


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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  
Followers 0