Sign in to follow this  
Followers 0
IanN1990

[Solved] Run timer outside of main loop

11 posts in this topic

#1 ·  Posted (edited)

Heya,

So basicly, when i click the desktop it shows it, when i "unclick it" it runs the time.exe which is a empty shell of code that lasts for 5 seconds and then hides the desktop. Is there any other way of going about this would wouldn't slow down the code. "if i put a sleep(5000) then the whole code is slowed down by 5 seconds whereas this way it will keep check every 500 mii seconds"

while 1
sleep(500)

if winactive("[Class:Progman]") Then
$hide=0
ControlShow("Program Manager", "", "[CLASS:SysListView32; INSTANCE:1]")
Else
if $hide=0 then
run("D\time.exe")
if proccessexist("time.exe") then
else
$hide=1
ControlHide("Program Manager", "", "[CLASS:SysListView32; INSTANCE:1]")
EndIf
EndIf
wend
Edited by IanN1990

Share this post


Link to post
Share on other sites



Put the above code in a function and call it every xxx milliseconds using AdlibRegister(function, xxx)


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

doesn't that mean it runs both scripts at the same time *main script and this function script" , doubling the cpu ? When i only want it to run a timer if when a condition ends ?

Share this post


Link to post
Share on other sites

AutoIt can't do two things at the same time. With AdlibRegister is stops execution of the main code every xxx milliseconds, calls the specified function and then returns to the 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

#6 ·  Posted (edited)

Hmm a intereseting function but that means every 250 sec my script loses focus for the check "as my script also runs lots of mousepos checks to show applications and taskbars but thanks for the post :D

@Blue i ran ur code, and it didn't really seam to do anything :S but the idea was soild and helped me with an answer :oops:

if winactive("[Class:Progman]") Then
$hide=0
$loop = 0
ControlShow("Program Manager", "", "[CLASS:SysListView32; INSTANCE:1]")
Else
if $hide=0 then
$loop = $loop + $num
if $loop = 5 Then
$hide=1
ControlHide("Program Manager", "", "[CLASS:SysListView32; INSTANCE:1]")
EndIf
EndIf
EndIf

Every main loop, it adds 1. So 10 loops = 500 per loop = 5 sec delay works nicely :rip:

Alot more so then running a extra exe ^^

Edited by IanN1990

Share this post


Link to post
Share on other sites

My code was example code showing you how to use the timerinit and timerdiff functions. Look them up in the help file. _DoSomething is a bogus function that does NOTHING because ... well ... the function calling it doesn't exist.

I'm not here to spoonfeed you code. Engage your brain and you'll figure it out....

My code will call the undefined function every 500ms or so ... you just have to write what it's bouncing out to go do.


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

If you change Blue_Drache's code to the code below it will show you how to delay it correctly, his code resets the timer every time through the loop.

$temp = TimerInit() ; this has to be outside the while loop
While 1
    if TimerDiff($temp) > 500 Then
      ConsoleWrite("Timer reset again" & @LF)
      $temp = TimerInit() ; Resets the timer
    Else
        sleep(10); so the script doesn't overload the CPU
    EndIf
WEnd

This will display a ConsoleWrite in SciTE if you run it from there.


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

#9 ·  Posted (edited)

I didn't mean it like that Blue :D I did add the function and name, and when that failed i replaced the function with a msgbox "as i use msgboxes for finding out where code is breaking" and nothing really happened. Thats what i ment :oops:

Your code with _DoSomething() replaced with a Msgbox. Running the code after 500 Mili seconds should make the msgbox appear but nothing happens :rip:

While 1
   Sleep(10)
   $temp = TimerInit()
   if TimerDiff($temp) > 500 Then
      MsgBox(4096, "Test", "This box will time out in 10 seconds", 10)
      $temp = TimerInit()
   EndIf
WEnd

Looking at BrewManNH post it does seam like the first TimerInIt() was in the wrong location, preventing the code from working correctly :) but its still nice to have a more professional timer coding then my old exe or loop ideas :D

Edited by IanN1990

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Oops, I was **SURE** I had that init outside the loop. My bad.

Sorry for jumping to grouchy conclusions.

Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

no harm done :D

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