Jump to content

AdlibRegister doesn't work in this script


Recommended Posts

Hello AutoIt Community

I'm fairly new to AutoIt while I have some experience in other general programming. I have this dilemma where an AdLibRegister function doesn't seem to be starting the given Function at all. I actually made the same working script before (and it looked quite glorious, I have to say), but I forgot to back it up before I formatted my drive.

This is the code that I'm having trouble with. I'm sure it's actually just a small mistake on my side that I can't identify.

StartProg()
Func StartProg()
    AdlibRegister("_ProgExists", 10000)
    If Not ProcessExists("Prog.exe") And Not ProcessExists("CDL.exe") Then
       Run("C:\Program Files (x86)\Prog\Prog.exe")
       ConsoleWrite("Started Prog" & @CRLF)
       Run("C:\Program Files (x86)\CDL\CDL.exe")
       ConsoleWrite("Started CDL" & @CRLF)
    ElseIf ProcessExists("Prog.exe") And Not ProcessExists("CDL.exe") Then
       Run("C:\Program Files (x86)\CDL\CDL.exe")
       ConsoleWrite("Started CDL" & @CRLF)
    EndIf
    ProcessWait("Prog.exe")
EndFunc
Func _ProgExists()
    Local Static $iCount = 0
    $iCount += 1
    If ProcessExists("Prog.exe") = 0 Then
       ProcessClose("CDL.exe")
       ConsoleWrite("CDL has been terminated." & @CRLF)
       AdlibUnRegister("_ProgExists")
    Else
       ConsoleWrite("Checked if Prog is running/CDL can be closed " & $iCount & " time(s)" & @CRLF)
    EndIf
EndFunc

I don't get any errors or such. The programs start normally but the AdLibRegister function does not seem to be doing anything here.

Thanks very much in advance,

~Medallyon

Edited by Medallyon
Link to comment
Share on other sites

You don't have a loop to keep this script alive, as soon as the process Prog.exe exists, the script ends.

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

So how do I create a loop?

I thought that the first AdLibRegister() would start running from the beginning of the script to create a loop that checks if Prog.exe has been ended.

I've tried changing the If in the _ProgExists Function to a while to create a loop, with no avail.

(Also, how do I get my code to be colourful and fancy like I've seen in other threads? xp)

Link to comment
Share on other sites

So how do I create a loop?

I thought that the first AdLibRegister() would start running from the beginning of the script to create a loop that checks if Prog.exe has been ended.

I've tried changing the If in the _ProgExists Function to a while to create a loop, with no avail.

(Also, how do I get my code to be colourful and fancy like I've seen in other threads? xp)

While 1
    StartProg()
WEnd

Actually, I don't know how this works with adlib, it may need tobe outside the loop

Edited by kaisies
Link to comment
Share on other sites

My memory is slightly coming back from when I made the first working script and I'm sure it looked a bit like this:

StartProg()

Func StartProg()
    ; Check if Prog is opened, if not will open Prog and CDL, otherwise just CDL.
    If Not ProcessExists("Prog.exe") And Not ProcessExists("CDL.exe") Then
       Run("C:\Program Files (x86)\Prog\Prog.exe")
       ConsoleWrite("Started Prog" & @CRLF)
       Run("C:\Program Files (x86)\CDL\CDL.exe")
       ConsoleWrite("Started CDL" & @CRLF)
    ElseIf ProcessExists("Prog.exe") And Not ProcessExists("CDL.exe") Then
       Run("C:\Program Files (x86)\CDL\CDL.exe")
       ConsoleWrite("Started CDL" & @CRLF)
    EndIf
EndFunc

ProcessWait("Prog.exe", 20)

; Register the function _ProgExists() to be called every 10s.
AdlibRegister("_ProgExists", 10000)

Func _ProgExists()
   While 1
      If ProcessExists("Prog.exe") = 0 Then
         ProcessClose("CDL.exe")
         ConsoleWrite("CDL has been terminated." & @CRLF)
         AdlibUnRegister("_ProgExists")
      EndIf
   WEnd

    ; Assign a static variable to hold the number of times the function is called.
    Local Static $iCount = 0
    $iCount += 1

    ConsoleWrite("Checked if Prog is running/CDL can be closed " & $iCount & " time(s)" & @CRLF)
EndFunc

 It still doesn't work. The script does not output anything other than "Started Prog" & "Started CDL" and "Exit code: 0    Time: 1.27". At this point I'm aiming to get the ConsoleWrite for my progressive $iCount into the console. I'm sure that if this will work, I can get my other code to work.

While 1
    StartProg()
WEnd

Actually, I don't know how this works with adlib, it may need tobe outside the loop

 This doesn't actually do anything, it just stalls the script until I force break it.

Edited by Medallyon
Link to comment
Share on other sites

Maybe something like this?

HotKeySet("{ESC}", "_Exit")
StartProg()

Func StartProg()
    ; Check if Prog is opened, if not will open Prog and CDL, otherwise just CDL.
    If Not ProcessExists("Prog.exe") And Not ProcessExists("CDL.exe") Then
        Run("C:\Program Files (x86)\Prog\Prog.exe")
        ConsoleWrite("Started Prog" & @CRLF)
        Run("C:\Program Files (x86)\CDL\CDL.exe")
        ConsoleWrite("Started CDL" & @CRLF)
    ElseIf Not ProcessExists("CDL.exe") Then
        Run("C:\Program Files (x86)\CDL\CDL.exe")
        ConsoleWrite("Started CDL" & @CRLF)
    EndIf
EndFunc   ;==>StartProg

ProcessWait("Prog.exe", 20)

; Register the function _ProgExists() to be called every 10s.
AdlibRegister("_ProgExists", 10000)
While 1
    Sleep(100)
WEnd

Func _ProgExists()
    If Not ProcessExists("Prog.exe") Then
        ProcessClose("CDL.exe")
        ConsoleWrite("CDL has been terminated." & @CRLF)
        AdlibUnRegister("_ProgExists")
        Exit ; <<<<<<<<< not sure if you want the script to close if you've done the above.
    EndIf
    ; Assign a static variable to hold the number of times the function is called.
    Local Static $iCount = 0
    $iCount += 1

    ConsoleWrite("Checked if Prog is running/CDL can be closed " & $iCount & " time(s)" & @CRLF)
EndFunc   ;==>_ProgExists
Func _Exit()
    Exit
EndFunc

 

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

try this way:

StartProg()

Func StartProg()
    ; Check if Prog is opened, if not will open Prog and CDL, otherwise just CDL.
    If Not ProcessExists("Prog.exe") And Not ProcessExists("CDL.exe") Then
        Run("C:\Program Files (x86)\Prog\Prog.exe")
        ConsoleWrite("Started Prog" & @CRLF)
        Run("C:\Program Files (x86)\CDL\CDL.exe")
        ConsoleWrite("Started CDL" & @CRLF)
    ElseIf ProcessExists("Prog.exe") And Not ProcessExists("CDL.exe") Then
        Run("C:\Program Files (x86)\CDL\CDL.exe")
        ConsoleWrite("Started CDL" & @CRLF)
    EndIf
EndFunc   ;==>StartProg

ProcessWait("Prog.exe", 20)
; Register the function _ProgExists() to be called every 10s.
AdlibRegister("_ProgExists", 10000)
While 1 ;  a loop to keep this script alive
    Sleep(1000)
WEnd
Func _ProgExists()
    ; While 1
    If ProcessExists("Prog.exe") = 0 Then
        ProcessClose("CDL.exe")
        ConsoleWrite("CDL has been terminated." & @CRLF)
        AdlibUnRegister("_ProgExists")
        Exit ; eventually
    EndIf
    ; WEnd

    ; Assign a static variable to hold the number of times the function is called.
    Local Static $iCount = 0
    $iCount += 1

    ConsoleWrite("Checked if Prog is running/CDL can be closed " & $iCount & " time(s)" & @CRLF)
EndFunc   ;==>_ProgExists

 

Edit:

... opss, nearly the same as KingBob, ...I'm slower in typing

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Maybe something like this?

HotKeySet("{ESC}", "_Exit")
StartProg()

Func StartProg()
    ; Check if Prog is opened, if not will open Prog and CDL, otherwise just CDL.
    If Not ProcessExists("Prog.exe") And Not ProcessExists("CDL.exe") Then
        Run("C:\Program Files (x86)\Prog\Prog.exe")
        ConsoleWrite("Started Prog" & @CRLF)
        Run("C:\Program Files (x86)\CDL\CDL.exe")
        ConsoleWrite("Started CDL" & @CRLF)
    ElseIf Not ProcessExists("CDL.exe") Then
        Run("C:\Program Files (x86)\CDL\CDL.exe")
        ConsoleWrite("Started CDL" & @CRLF)
    EndIf
EndFunc   ;==>StartProg

ProcessWait("Prog.exe", 20)

; Register the function _ProgExists() to be called every 10s.
AdlibRegister("_ProgExists", 10000)
While 1
    Sleep(100)
WEnd

Func _ProgExists()
    If Not ProcessExists("Prog.exe") Then
        ProcessClose("CDL.exe")
        ConsoleWrite("CDL has been terminated." & @CRLF)
        AdlibUnRegister("_ProgExists")
        Exit ; <<<<<<<<< not sure if you want the script to close if you've done the above.
    EndIf
    ; Assign a static variable to hold the number of times the function is called.
    Local Static $iCount = 0
    $iCount += 1

    ConsoleWrite("Checked if Prog is running/CDL can be closed " & $iCount & " time(s)" & @CRLF)
EndFunc   ;==>_ProgExists
Func _Exit()
    Exit
EndFunc

 

Yes this works!

Thanks for all the input from everyone. I've tested both Scripts and only the one above from @KingBob worked, althought I don't actually see a difference in @Chimp's Script. Eh~.

Thanks again,

~Medallyon

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...