Jump to content
aa2zz6

How can I get this While loop for multiple functions to work.

Recommended Posts

aa2zz6

So I've written a program and I tried adding a while sleep loop instead of using a task scheduler so wrote a simple while loop to sleep and every 20 minutes it would repeat two functions. I'll post the code below if anyone have suggestions. Thanks for any feedback. 

The While 1 is supposed to loop Application1 and Application2 every 20mins.

While 1
   Application1()
   Application2()
   Sleep(1200000 ) ; loop every 1200000 seconds = 20mins
WEnd

Func Application1() 
If @WDAY >= 2 And @HOUR = 18 And @MIN = 0 Then

If pixelgetcolor(38, 280) = 0x6AB622 then ;IF [Application1] - BLUE was found. Software is running;
   sleep(5000)
MsgBox($MB_OK, "Application1", "The application is running", 5)
sleep(5000)
EndIf
If Not pixelgetcolor then ;IF NOT COLOR BLUE THEN CALL Application1;
sleep(5000)
Call("Application1")
sleep(8000)
EndIf
EndIf

EndFunc

Func Application2()
If @WDAY >= 2 And @HOUR = 18 And @MIN = 0 Then

If pixelgetcolor(37, 310) = 0x32BEF2 then ;IF Application[2] - BLUE was found. Software is running;
   sleep(5000)
MsgBox($MB_OK, "Application2", "The application is running", 5)
sleep(5000)
endif

If Not pixelgetcolor then ;IF NOT COLOR BLUE THEN CALL Application2;
sleep(5000)
Call("Application2")
sleep(5000)
EndIf
EndIf

EndFunc

 

Edited by aa2zz6
Displayed code as colored text

Share this post


Link to post
Share on other sites
TheSaint

Hi. Welcome.

You should be using proper code highlighting. You can do that using the <> button, to the left of the smiley face button.
That makes it much easier for others to troubleshoot your code.

While 1
   Application1()
   Application2()
   Sleep(1200000 ) ; loop every 1200000 seconds = 20mins
WEnd

Func Application1() 
If @WDAY >= 2 And @HOUR = 18 And @MIN = 0 Then

If pixelgetcolor(38, 280) = 0x6AB622 then ;IF [Application1] - BLUE was found. Software is running;
   sleep(5000)
MsgBox($MB_OK, "Application1", "The application is running", 5)
sleep(5000)
EndIf
If Not pixelgetcolor then ;IF NOT COLOR BLUE THEN CALL Application1;
sleep(5000)
Call("Application1")
sleep(8000)
EndIf
EndIf

EndFunc

Func Application2()
If @WDAY >= 2 And @HOUR = 18 And @MIN = 0 Then

If pixelgetcolor(37, 310) = 0x32BEF2 then ;IF Application[2] - BLUE was found. Software is running;
   sleep(5000)
MsgBox($MB_OK, "Application2", "The application is running", 5)
sleep(5000)
endif

If Not pixelgetcolor then ;IF NOT COLOR BLUE THEN CALL Application2;
sleep(5000)
Call("Application2")
sleep(5000)
EndIf
EndIf

EndFunc

I've only had a quick look at your code, but it seems apparent to me, that you may have a timing issue, specifically as the use of @MIN = 0 is very specific and limiting.

You can of course, test that element by displaying it in a Msgbox.

I suggest you have a go at trying to come up with a solution, bearing that in mind.
Perhaps using the time plus a much smaller sleep rather than just sleep in your function calling loop.

 

Edited by TheSaint

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites
aa2zz6

I was doing a little more research, learned something new, and here is what I found out. This seems to work but I'm not sure about this piece of code:

If TimerDiff($iTimer) >= 1 * 1 * 2 Then
$iTimer = TimerInit()
While 1
     If TimerDiff($iTimer) >= 1 * 1 * 2 Then
         wave1()
         wave2()
     EndIf
     Sleep(900000)
WEnd

 

Edited by aa2zz6
There was a typo in the code. Needed corrected.

Share this post


Link to post
Share on other sites
kylomas

aa2zz6,

You are testing for a difference of 2 milli seconds, doing something then sleeping for 15 minutes.  Is this what you intended?  You also have no way of ending the loop.  If you explain what you are trying to do then we are in a better position to advise.

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
jvds

if you are trying to use the timerinit instead of sleep to run your code every 20 mins then try this
 

$iTimer = TimerInit()
$minutes = 1 ;the timer will trigger at this amount of minutes

While 1
    If TimerDiff($iTimer) / 1000/60 >= $minutes Then
        $iTimer = TimerInit() ;reset timer so it can trigger again in X minutes set at $minutes
        consolewrite ("!TIMER was triggered"&@LF)
;~      wave1()
;~      wave2()
    EndIf
;~  consolewrite ("diff="&TimerDiff($iTimer)/1000/60&@crlf)
    Sleep(1000)
WEnd

 

  • Like 1

Share this post


Link to post
Share on other sites
TheSaint

In your first post, both functions run under the same conditions, which you test for twice, but really only need to do once.

So you could just use that test

If @WDAY >= 2 And @HOUR = 18 And @MIN = 0 Then

In your While 1...Wend and then have both functions run one after the other.

While 1
   If @WDAY >= 2 And @HOUR = 18 And @MIN = 0 Then
        Application1()
        Application2()
   EndIf
   Sleep(1000) ; loop every 1 second
WEnd

You can of course adjust the Sleep to whatever is appropriate.

I was hoping you would understand enough from my first post to determine that.

Edited by TheSaint

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites
aa2zz6

Thanks the information was very helpful.

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

×