Jump to content

running two random sleep loops at one time?


Recommended Posts

I was attempting to run two different functions simultaneously, at first i was just using two different scripts, but they seem to conflict with each other too often. i did some digging and found the following.

https://www.autoitscript.com/forum/topic/19521-running-2-loops-at-the-same-time/

the only problem is im having issues getting this to use a sleep(random) to go with my script.

Spoiler
HotKeySet("{ESC}", "Terminate")
HotKeySet("{F2}", "_F2a_Func")
HotKeySet("{F3}", "_F3a_Func")
HotKeySet("{F4}", "_F4a_Func")
HotKeySet("{F5}", "Pause")

Local $word[5] = ["random1", "random2", "random3", "random4", "random5"]

Func _F2a_Func()
While 1
    If $Timer1 = '' Or TimerDiff($Timer1) > $Delay1 Then
        $Timer1 = TimerInit()
        _F3a_Func()
    EndIf
    If $Timer2 = '' Or TimerDiff($Timer2) > $Delay2 Then
        $Timer2 = TimerInit()
        _F4a_Func()
    EndIf
; Briefly pause to avoid excessive CPU usage
    Sleep(100)
WEnd
EndFunc

Func _F3a_Func()
Send($word[Random(0, UBound($word) -1, 1)] & "{ENTER}")
Sleep(Random(3000, 5000, 1))
EndFunc

Func _F4a_Func()
Send("function two")
Sleep(Random(60000, 90000, 1))
EndFunc

Func Pause()
    While 1
        Sleep(50)
    WEnd
EndFunc

Func Terminate()
    Exit 0
EndFunc

While 1
    Sleep(10);idle around
WEnd

 

if anyone can help me solve this, it would be much appreciated.

 

edit:

second failed example of trying to code it in

Spoiler
HotKeySet("{ESC}", "Terminate")
HotKeySet("{F2}", "_F2a_Func")
HotKeySet("{F3}", "_F3a_Func")
HotKeySet("{F4}", "_F4a_Func")
HotKeySet("{F5}", "Pause")

Local $word[5] = ["random1", "random2", "random3", "random4", "random5"]

Func _F2a_Func()

Local $Timer1, $Delay1 = Sleep(Random(1000, 1100, 1))
Local $Timer2, $Delay2 = Sleep(Random(30000, 31000, 1))

While 1
    If $Timer1 = '' Or TimerDiff($Timer1) > $Delay1 Then
        $Timer1 = TimerInit()
        _F3a_Func()
    EndIf
    If $Timer2 = '' Or TimerDiff($Timer2) > $Delay2 Then
        $Timer2 = TimerInit()
        _F4a_Func()
    EndIf
; Briefly pause to avoid excessive CPU usage
    Sleep(100)
WEnd
EndFunc

Func _F3a_Func()
Send($word[Random(0, UBound($word) -1, 1)] & "{ENTER}")
EndFunc

Func _F4a_Func()
Send("function two")
EndFunc

Func Pause()
    While 1
        Sleep(50)
    WEnd
EndFunc

Func Terminate()
    Exit 0
EndFunc

While 1
    Sleep(10);idle around
WEnd

 

it waits until timer two's delay finishes, then just infinitely spams.

Edited by Bobm
added second failed example of my code
Link to comment
Share on other sites

What is the problem, exactly?

Try setting the random prior to the sleep, have the random in a variable and place the variable in the sleep, see if it makes any difference.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

20 hours ago, careca said:

What is the problem, exactly?

Try setting the random prior to the sleep, have the random in a variable and place the variable in the sleep, see if it makes any difference.

so far when ive tried to add

Local $Timer1, $Delay1 = sleep(random)

it seems to wait until it reaches that time, then just infinitely spams

what im trying to have it do, is to repeatedly do the random sleep of f3 until its time for it to do f4, but so far when ive tried it seems to just be spamming both repeatedly without the properly sleep times

Link to comment
Share on other sites

You  can't run 2 Sleeps at the same time, the whole script will pause for the first sleep time, and then continue.

Use timers and not sleep.

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

Link to comment
Share on other sites

What i meant for you to try:

$var = Random(3000, 5000, 1)
Sleep($var)

Now that i think of it, shouldn't make a difference.

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

4 hours ago, BrewManNH said:

You  can't run 2 Sleeps at the same time, the whole script will pause for the first sleep time, and then continue.

Use timers and not sleep.

thats what im attempting to figure out, is how to get the 2 random sleeps to work as random timers or vice versa, however it works.

 

4 hours ago, FrancescoDiMuro said:

Hi @Bobm, and welcome to the AutoIt forum :)
Post the code you are working on, so we can see what you've tried so far :)

Best Regards.

its in the spoiler in my first post

Link to comment
Share on other sites

Things i dont get: $Delay1 is never defined, how can the script check TimerDiff($Timer1) > $Delay1 if it doesn't know what the delay is?

And i didn't get the point of all this

 

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

As @BrewManNH explained in his post, and as it is explained in the Microsoft Documentation, the Sleep() function pauses the current thread, and, since AutoIt is not multi-thread, it will pause the entire script.

@Bobm You can't run two Sleep() functions at the same time.

 

Best Regards.

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Can you please explain the problem you want to solve?
So we can understand if two functions need to run at the same time or if another solution fits your needs.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

2 hours ago, water said:

Can you please explain the problem you want to solve?
So we can understand if two functions need to run at the same time or if another solution fits your needs.

yes, trying to get both function f3 and function f4 to work at the same time essentially, but i want them to both be running on random sleep times / delays. i want f3 to run continuously until its time for f4 to happen, pausing f3 until f4 finishes, and then return to f3 until its time for f4 again.

Link to comment
Share on other sites

2 things:

1 - Dont quote a post directly above, it's anoying.

2 - That is not "running 2 functions simultaneously", that is alternating between 2 functions.

HotKeySet("{ESC}", "Terminate")
HotKeySet("{F2}", "OnOff")
;=============================================================================
Local $Timer1, $RdmTime
Local $On = 0
;=============================================================================
Func OnOff()
    If $On = 1 Then
        $On = 0
    Else
        $On = 1
    EndIf
EndFunc   ;==>OnOff
;=============================================================================
Func _F3a_Func()
    ConsoleWrite('_F3a_Func - ' & @MSEC & @CRLF)
EndFunc   ;==>_F3a_Func
;=============================================================================
Func _F4a_Func()
    ConsoleWrite('_F4a_Func - ' & @MSEC & @CRLF)
EndFunc   ;==>_F4a_Func
;=============================================================================
While 1
    If $On = 1 Then
        ;=============================================================================
        $RdmTime = Random(1000, 4000, 1)
        $Timer1 = TimerInit()
        Do
            If $On = 0 Then
            ExitLoop
            EndIf
            _F3a_Func()
            Sleep(100)
        Until TimerDiff($Timer1) > $RdmTime
        _F4a_Func()
        ;=============================================================================
    EndIf
    Sleep(100)
WEnd
;=============================================================================
Func Terminate()
    Exit 0
EndFunc   ;==>Terminate
;=============================================================================

 

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

7 hours ago, Bobm said:

yes, trying to get both function f3 and function f4 to work at the same time essentially, but i want them to both be running on random sleep times / delays. i want f3 to run continuously until its time for f4 to happen, pausing f3 until f4 finishes, and then return to f3 until its time for f4 again.

You do not explain the problem, you explain the solution you try to implement.

Explaining the problem would be like:
"I have a script that runs application x. Sometimes a popup gets displayed. I need a function to wait for this popup and click a button"

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@careca this seems to be almost perfect, plenty better than what i had. i notice that hitting the pause still has f4 go off, is this unavoidable?

i apologize, im not the best at explaining things. but you appear to understand what i was attempting to say. i really appreciate the help you have given.

@water

apologies, properly explaining things isn't one of my strengths. but i briefly mentioned in my original under the edit, and my first reply, of the problem being it was infinitely spamming. it seems i didnt quite make the issue of it quite clear. but it was spamming without any delay, and i was trying to get it to have proper delays.

Edited by Bobm
to make water happy. excuse my failed attempt to use the quotes for directing my response.
Link to comment
Share on other sites

We do not expect a PERFECT explanation, but we do expect that you TRY to explain your PROBLEM.

Quote

Dont quote a post directly above, it's anoying.

What do you do in your next post?  You quote the post again :doh:

As it seems you do not read our replies and just keep repeating your initial post I'm out now!

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

12 hours ago, Bobm said:

@careca  i notice that hitting the pause still has f4 go off, is this unavoidable?

Hi, No. Try this one:

HotKeySet("{ESC}", "Terminate")
HotKeySet("{F2}", "OnOff")
;=============================================================================
Local $Timer1, $RdmTime
Local $On = 0
;=============================================================================
Func OnOff()
    If $On = 1 Then
        $On = 0
    Else
        $On = 1
    EndIf
EndFunc   ;==>OnOff
;=============================================================================
Func _F3a_Func()
    ConsoleWrite('_F3a_Func - ' & @MSEC & @CRLF)
EndFunc   ;==>_F3a_Func
;=============================================================================
Func _F4a_Func()
    ConsoleWrite('_F4a_Func - ' & @MSEC & @CRLF)
EndFunc   ;==>_F4a_Func
;=============================================================================
While 1
    If $On = 1 Then
        ;=============================================================================
        $RdmTime = Random(1000, 4000, 1)
        $Timer1 = TimerInit()
        Do
            _F3a_Func()
            Sleep(100)
        Until TimerDiff($Timer1) > $RdmTime Or $On = 0
        If $On = 1 Then
        _F4a_Func()
        EndIf
        ;=============================================================================
    EndIf
    Sleep(100)
WEnd
;=============================================================================
Func Terminate()
    Exit 0
EndFunc   ;==>Terminate
;=============================================================================

 

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

@careca yes that worked, thank you. I think if i add another OnOff in there, i could perhaps pause f3 but keep f4 to continue after yes? or vice versa. i will attempt this when i get some time later.

im still very much a noob as they say. i used to know a bit more, but i havent done anything with coding in so long i have forgotten things.

 

Edited by Bobm
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...