Jump to content
Sign in to follow this  
6DAVEROO9

Getting Started

Recommended Posts

6DAVEROO9

Hi Folks,

While I am new to AutoIt, as well as to Windows programming, I do have a fairly solid understanding of programming structures (IF's, WHILE's, FOR's, etc.)

What I am trying to accomplish is the following:

Poll system time, and when it reaches 2:10 AM, launch a program.

Continue polling system time, and when it reaches 6:50 AM close the program that was launched at 2:10.

That's it. I tried to schedule it in Task Manager, and while it launched, there does not seem to be a way to shut the program down, other than the vague "If still running after" option. I also tried to search online for an automation program that would provide the option to launch and close other programs, and that is what lead me to AutoIt. That's fine. I have no problem learning how to do it myself. I just need some guidance with respect to which variables to use, and the syntax of the AutoIt commands. All help is appreciated.

Cheers.

~6DAVEROO9~

Share this post


Link to post
Share on other sites
stormbreaker

Welcome to the forum. This should do the work:

While 1
If @Hour = 02 and @Min = 10 then
If ProcessExists("cmd.exe") then
_DoNothing()
else
Run("cmd.exe")
EndIf
EndIf
If @Hour = 06 and @Min = 50 then
ProcessClose("cmd.exe")
ExitLoop
EndIf
WEnd
Func _DoNothing()
EndFunc

Here CMD.exe is the name of my program.

Edited by MKISH

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Share this post


Link to post
Share on other sites
6DAVEROO9

Thanks MKISH. That looks way more like something I'm familiar with than what I was doing. Once you know the syntax, it just flows right out, doesn't it?

Share this post


Link to post
Share on other sites
stormbreaker

Yup. Once you master this one, it helps you a lot even in other languages like C/C++ as well


----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Share this post


Link to post
Share on other sites
armoros

Welcome to the forum. This should do the work:

While 1
If @Hour = 02 and @Min = 10 then
If ProcessExists("cmd.exe") then
_DoNothing()
else
Run("cmd.exe")
EndIf
EndIf
If @Hour = 06 and @Min = 50 then
ProcessClose("cmd.exe")
ExitLoop
EndIf
WEnd
Func _DoNothing()
EndFunc

Here CMD.exe is the name of my program.

Man this snippet cleared a lot of stuff in my mind if-else-run... must go in Examples Script Wiki.....

[font="verdana, geneva, sans-serif"] [/font]

Share this post


Link to post
Share on other sites
stormbreaker

Glad it was helpful to you guys... These days I am getting busy a lot with the cafe work. We are actually upgrading all our PC's and installing new software etc.


----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Share this post


Link to post
Share on other sites
Zedna

Welcome to the forum. This should do the work:

...

Here is optimized way:

Global $is_running = False

While 1
    If @HOUR = 02 And @MIN = 10 Then
        If Not $is_running Then
            If Not ProcessExists("cmd.exe") Then Run("cmd.exe")
            $is_running = True
        EndIf
    EndIf
    
    If @HOUR = 06 And @MIN = 50 Then
        If $is_running Then
            ProcessClose("cmd.exe")
;            $is_running = False ; not needed
            ExitLoop
        EndIf
    EndIf
    
    Sleep(1000)
WEnd

Share this post


Link to post
Share on other sites
Exit

more optimized ?

While 1
If @HOUR & @MIN = 0210 Then
  $pid = Run("cmd.exe")
  Sleep(16800000)  ; wait 4 hours and 40 minutes
  ProcessClose($pid)
  Sleep(69590000)  ; wait 19 hours and 19 minutes and 50 seconds. Allow 10 seconds timing difference due to start/stop process.  
EndIf
Sleep(1000)
WEnd

My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post


Link to post
Share on other sites
6DAVEROO9

Gee, thanks for all the optimizations, guys. I am kind of going in another direction, however. I found that the program was using up 25% CPU time, likely because it was polling the system time continuously while waiting for the criteria to be met. I put the following together in an attempt to minimize activity:

A = @HOUR
B = @MIN
If A > 6
   C = 25 - A
   E = 290
ElseIf A < 2
   C = A - 1
   E = 290
Else
   C = 0
   B = 70
   E = ( ( 60 * ( 5 - A ) ) + ( 70 - B )
EndIf
D = ( C * 60 ) + ( 70 - B )
Wait D minutes
Run ( "C:\Program.exe" )
Wait E minutes
ProcessClose ( "Program.exe" )

What do you think?

Share this post


Link to post
Share on other sites
PhoenixXL

Hey 6DAVEROO9

You can Try AdlibRegister to Poll Each Minute

It will not Trap You In A Loop

MoreOver it Will Consume Less CPU

Code

AdlibRegister('_MyFunc',60000)
Func _MyFunc()
    If @HOUR = 02 And @MIN = 10 Then
       If Not ProcessExists("cmd.exe") Then Run("cmd.exe")
    ElseIf @HOUR = 06 And @MIN = 50 Then
       If ProcessExists("cmd.exe") Then ProcessClose("cmd.exe")
    EndIf
EndFunc
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
6DAVEROO9

Thanks PhoenixXL. I like it. Very elegant. Another example of how it pays to know your process library.

Share this post


Link to post
Share on other sites
Mellon

Unless there is a crying need to have this running all the time I would skip the first part and just use the Windows scheduler to start the thing.

Share this post


Link to post
Share on other sites
6DAVEROO9

Oops!

Just compiled and ran it, and nothing appears to have happened. Cannot see the process running in Task Manager. Wil try to rework mine in the meantime to see if I can get better results.

Share this post


Link to post
Share on other sites
BrewManNH

Unless you have a While loop in there or a Do loop, the script is going to immediately run and close.


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
6DAVEROO9

OK, so putting in the While loop on PhoenixXL's script got it compiled and running. I was still somewhat frustrated that I could not get my own script running, so I plugged away at it, and I finally came up with this. Launch at bedtime, and forget. It polls the system only once, and then does its thing.

Dim $A=@HOUR, $B=@MIN, $C, $D, $E
While 1
_CalcInterval ()
$D=(($C*60)+(70-$B))*60000
Sleep($D)
Run("C:\Program Files (x86)\Steam\Steam.exe")
Sleep($E)
ProcessClose("Steam.exe")
WEnd
Func _CalcInterval ()
If $A > 6 Then
   $C = 25 - $A
   $E = 17400000
ElseIf $A < 2 Then
   $C = $A - 1
   $E = 17400000
Else
   $C=0
   $B=70
   $E=((60*(5-$A))+(70-$B))*60000
EndIf
EndFunc

My first functioning script! Newbie challenge #1... Accepted. And completed. F#ck yeah.

OK, maybe I've been spending a bit too much time at Memebase. Time to go outside. ;-)

Oh, and if I didn't mention it, the reason I needed this is that my ISP has unlimited downloads during that time window, so it is the best time to download game files.

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  

×