Sign in to follow this  
Followers 0
nolongthin

Using two continuous loop in a script

33 posts in this topic

Hello am a newbie to autoit,

I have a script that is meant to run continuously, now i want the script to send the file to my email, I have implemented INetSmtpMailCom but the problem is when i want to use a timer to tell it when to send the file, i have use another while infinite loop and if i put it in the first while infinite loop it wont send because the first while loop is still using the file.

If i put it outside the first while infinite loop, its stop the first loop and only the second loop runs.I"ve tried putting the timer loop in another script and including it into the first but i still encounter the same problem

Pleas any ideas on how to implement two infinite loops in a script.

thanks

Share this post


Link to post
Share on other sites



Infinite by definition means never ending, you can't have two endless loops, you should post your code and we can see what you need to get it working.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Func _TimerDiff()
   _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
  AdlibEnable(_TimerDiff(), 300000 )  ; call this function again in 1 minute ; call this function again in 1 second
EndFunc

_TimerDiff()

I decided to use adlib i want it to send every 5minutes but it doesnt work instead it sends every second

please help

Share this post


Link to post
Share on other sites

You should use AdLibEnable at the beginning of your script - not in the function that is called by AdLibEnable.

You shouldn't name your functions like functions used by AutoIt or its UDFs. Your name (_TimerDiff) is only a "_" away from _Timer_Diff. That might get confusing.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

AdlibEnable(_Timer(), 10000)  ; call this function again in 1 minute ; call this function again in 1 second
Func Timer()
   Local $iMinutes = 1
   Local $hTimer = TimerInit()
     If TimerDiff($hTimer) > ($iMinutes * 60000) Then _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
  AdlibEnable(_Timer(), 10000)  ; call this function again in 1 minute ; call this function again in 1 second
EndFunc

_TimerDiff()

like this?

Share this post


Link to post
Share on other sites

sorry that was a mistake

AdlibEnable(Timer(), 10000)  ; call this function again in 1 minute ; call this function again in 1 second
Func Timer()
   Local $iMinutes = 1
   Local $hTimer = TimerInit()
     If TimerDiff($hTimer) > ($iMinutes * 60000) Then _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
 
EndFunc

Timer()

If i do this, i get this error:

AdlibEnable(Timer(), 10000)

^ ERROR

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Try this:

AdlibRegister("Timer", 60 * 1000) ; Call this function every 60 seconds
; AdlibEnable("Timer", 60 * 1000) ; Call this function every 60 seconds - AutoIt version < 3.3.2.0

; Here goes the main part of your script which will be interrupted every 60 seconds to call function "Timer".

Func Timer()
    _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
EndFunc   ;==>Timer

BTW: You seem to run an old version of Autoit. AdLibEnable has been replaced with AdLibRegister/AdLibUnregister in version 3.3.2.0.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#include <misc.au3>

AdlibRegister(Timer(), 60 * 1000) ; Call this function every 60 seconds

Func Timer()
    _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)

EndFunc   ;==>Timer

Hi, thanks for your suggestion, i created a new script and put this into it, but it runs once and exits, same thing happens when i put it in the main script

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

There has to be some code that keeps the script "busy". My example just starts and ends. You have to put in some code.

This example runs forever and should call the function every 6 seconds to write the time to the console:

#include <misc.au3>
AdlibRegister("Timer", 6 * 1000) ; Call this function every 60 seconds
While 1
WEnd

Func Timer()
    ConsoleWrite(@Min & ":" & @SEC & @CRLF)
EndFunc   ;==>Timer

Important:

You have to use

AdlibRegister("Timer", 6 * 1000)
not
AdlibRegister(Timer(), 6 * 1000)
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

AdlibRegister("Timer", 60 * 1000) ; Call this function every 60 seconds

Func Timer()
    _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)

EndFunc   ;==>Timer

Did that and it didn't send at all

Share this post


Link to post
Share on other sites

there is a previous while 1 in the main code, if i put the second it interrupts the first, and if the first still work the second would not it would give error that the process is being used by another

Share this post


Link to post
Share on other sites

The While loop was just an example! Replace the While loop I posted with your main code.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

AdlibRegister("Timer", 60 * 1000) ; Call this function every 60 seconds

Func Timer()
    _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)

EndFunc   ;==>Timer

Did that and it didn't send at all

Do you get an return code or is @error set by _INetSmtpMailCOM?

Try to insert a

ConsoleWrite("***" & @CRLF)
at the begin of the function.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

nolongthin here's a simple demonstration of how to handle nested loops. Hopefully it will give you an insight as to what is happening in your more complicated example. The best solution is still probably the one suggested by water.

MsgBox(0, "Msg from startup", "About to enter the first loop")

While 1 ; Loop 1
    MsgBox(0, "Msg from 1st Loop", "About to enter the second loop")

    While 1 ; Loop 2
        $ret = MsgBox(2, "Msg from 2nd Loop", "What do you want to do?")
        If $ret = 3 Then ; Abort
            ExitLoop 2 ; Get out of both loops
        ElseIf $ret = 4 Then ; Retry
            ExitLoop ; Only quit the second loop
        EndIf
    WEnd

WEnd

MsgBox(0, "Final Msg", "Aborted by user")

Notice what happens when you click Ignore!

Edited by czardas

Share this post


Link to post
Share on other sites

Hi still dint work gives this error

Description:The process cannot access the file because it is being used by another process.

Share this post


Link to post
Share on other sites

nolongthin,

Do you REALLY want to email a file to yourself every minute that this script is running? Perhaps there is another solution to what you are trying to do.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

@water i didn't it just

Exit code: 0

Share this post


Link to post
Share on other sites

@kylomas

Actually i want to email the file to myself every 30min that the script is run but am just testing it with 1 minute

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

kylomas is right, I think you need to tell us more about this. What exactly are you sending and why?

Edited by czardas

Share this post


Link to post
Share on other sites

nolongthin,

I see. The resource (file) is in use by another process (probably your email client). You might try waiting longer or making sure that all handles are closed.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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