Jump to content

Counting Functioncalls before code execution


Recommended Posts

Hi all,

I've been using AutoIt for a few months (nearly a year now, actually) and slowly getting the hang of it. (I've never been a great programmer!)

Anyway, I've now got a script that calls certain functions a total of 20 times, and I'm using those calls as my indicator of progress (20 works out to be a convenient 5% completion increase after each one).

But, if that number changes, I need to change my 20 to 21 (or whatever) by hand.

Is there a way to have the scrpt work out how many times those functions are called, before calling them? Can it step through itself and tally the number of times FunctionX is called before running?

Does that make sense?

Should I give it up as a foolish impossible dream?

Thanks in advance...

Have Fun...

nikink

Link to comment
Share on other sites

  • Moderators

Global $Func_Tally_CountCalls = 0

While 1
    Sleep(500)
    Tally()
    ToolTip('Current Tally() Function Calls is: ' & $Func_Tally_CountCalls, 0, 0)
    If $Func_Tally_CountCalls = 20 Then 
        MsgBox(64, 'Info:', 'You have reached ' & $Func_Tally_CountCalls & ' to Tally()')
        Exit
    EndIf
WEnd

Func Tally()
    $Func_Tally_CountCalls += 1
EndFunc
Have the function do it for you as the first thing it does when you call, and just make that variable global.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Global $Func_Tally_CountCalls = 0

While 1
    Sleep(500)
    Tally()
    ToolTip('Current Tally() Function Calls is: ' & $Func_Tally_CountCalls, 0, 0)
    If $Func_Tally_CountCalls = 20 Then 
        MsgBox(64, 'Info:', 'You have reached ' & $Func_Tally_CountCalls & ' to Tally()')
        Exit
    EndIf
WEnd

Func Tally()
    $Func_Tally_CountCalls += 1
EndFunc
Have the function do it for you as the first thing it does when you call, and just make that variable global.
So my functions would have Tally() as the first thing they did, thus generating a count of how many times they were called?

Not sure that would count the functions before they were called...

Here's a bit of an example:

Func DoSomething($var)
blahblah...
EndFunc

Func CollectionOfCommands()
...
DoSomething(stuff)
DoSomething(stuff)
...
EndFunc

So, I have 20x DoSomething which I use for calculating the progress of my script towards completion.

If I understand you, you are suggesting placing Tally() inside DoSomething(), but for my progress bar to work, I need to know how many times DoSomething() is called before the script has reached it's first DoSomething().

Make sense?

Link to comment
Share on other sites

  • Moderators

Tally would be the name of your function, and you would have a global variable for each function so you could distinguish apart from one another.

Edit:

These are only examples of what you asked, to keep track of how many times a function was called.

Global $Func_One_CountCalls = 0, $Func_Two_CountCalls = 0

While 1
    Sleep(500)
    FunctionOne()
    If Mod($Func_One_CountCalls, 2) = 0 Then FunctionTwo()
    ToolTip('Current FunctionOne() Function Calls is: ' & $Func_One_CountCalls, 0, 0)
    If $Func_One_CountCalls = 20 Then 
        MsgBox(64, 'Info:', 'You have reached ' & $Func_One_CountCalls & ' to FunctionOne()' & @CR & 'FunctionTwo() was called: ' & $Func_Two_CountCalls & ' times')
        Exit
    EndIf
WEnd

Func FunctionOne()
    $Func_One_CountCalls += 1
    ;Do something here
EndFunc

Func FunctionTwo()
    $Func_Two_CountCalls += 1
    ;Do something here
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

...keep track of how many times a function was called.

But I think I need to know how many time a function will be called.

I will see what I can do from those examples. I do appreciate your help here!

Link to comment
Share on other sites

  • Moderators

But I think I need to know how many time a function will be called.

I will see what I can do from those examples. I do appreciate your help here!

Um:)... won't a function only be called for the conditions you set? I mean that being the case, you control how many times it is called or will be called. I gave you an example of making sure that FunctionTwo() was only called 10 times vs FunctionOne() being called 20 times.

So wouldn't it be as simple as:

If $Func_One_CountCalls < 21 Then 
    FunctionOne()
EndIf
?

Edit:

In other words you know that it will only ever be called 20 times...

If this is still not what you need, then post your code, without it I won't help anymore because I don't understand what you want. (Be sure to comment it on what your trying to accomplish).

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Yah... I'm not being very clear, sorry. I'll try to explain what's going on in my head... :)

Global $CompletedFunctionCalls = 0
Global $TotalFunctionCalls = 5

Func DoThisThing()
...
...
EndFunc

Func DoThatThing()
...
...
EndFunc

Func DoAllTheseThings()
   DoThisThing()
   ShowProgress()
   DoThisThing()
   ShowProgress()
   DoThisThing()
   ShowProgress()
EndFunc

Func DoAllThoseThings()
   DoThatThing()
   ShowProgress()
   DoThatThing()
   ShowProgress()
EndFunc

Func ShowProgress()
   $CompletedFunctionCalls += 1
   $i = ($CompletedFunctionCalls / $TotalFunctionCalls) * 100
   ProgressSet( $i, $i & " percent complete.")
EndFunc

;;; Main;;;

ProgressOn("Progress Meter", "Doing Things", "0 percent")

DoAllTheseThings()
DoAllThoseThings()

ProgressSet(100 , "Done", "Complete")
sleep(500)
ProgressOff()

Ok, So I know the total number of times the script DoesThis and DoesThat (5) by just counting. But, eventually, the number of times it DoesThis and DoesThat will change.

It will only change when I alter the script, not during the running of the script itself.

I'm trying to get $TotalFunctionCalls to update itself from within the script, rather than have me (or whoever) remember to recount whenever the script is altered.

Is this possible? Or am I an idiot? (Highly likely... :( )

I suspect now, that I would have to craft DoAll[These|Those]Things as an array, where each cell is the function call to DoThisThing()... Possible?

Please feel free to say my code structure is awful and that there is a better way, I admit I'm not a natural at this stuff. :D

Thanks again.

Link to comment
Share on other sites

  • Moderators

LOL, I have no idea what that script is supposed to do :) Comments inside of it of what you are trying to accomplish would be great.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

LOL, I have no idea what that script is supposed to do :D Comments inside of it of what you are trying to accomplish would be great.

Ahem, yeah, sorry 'bout that... :)

Global $NumberOfCompletedFunctionCalls = 0
Global $TotalNumberOfFunctionCalls = 5

Func DoThisThing()
  ; This is a repetitive task that has to be done, but is not the same task as in DoThatThing
...
EndFunc

Func DoThatThing()
  ; This is a repetitive task that has to be done, but is not the same task as in DoThisThing
...
EndFunc

Func DoAllTheseThings()
  ; This function calls all the repetitive tasks that are done via DoThisThing(), all of which must be done before AllThoseThings are done

   DoThisThing()
   ShowProgress(); This is where I update the progressbar. After 'ThisThing' has been done, which is one of five total number of function calls... so progressbar works out to be 20%
   
   DoThisThing()
   ShowProgress(); As above, but the second one, so progressbar becomes 40%
   
   DoThisThing()
   ShowProgress(); As above but the third one, so progressbar becomes 60%. 

  ; At this point 'AllTheseThings' are done and the script is 60% complete as calculated by 
  ; the number of tasks to do vs the number of tasks done so far...

EndFunc

Func DoAllThoseThings()
  ; This function completes the script by running a number of tasks that may only be done after 'AllTheseThings' have been completed.

   DoThatThing()
   ShowProgress(); This is where I update the progressbar. After 'ThatThing' has been done, which is one of five total number of function calls... so progressbar works out to be 80%

   DoThatThing()
   ShowProgress(); As above, but progressbar goes to 100% 

  ; Of course, as you can see, this is that last thing that needs doing. All Things have been done!

EndFunc

Func ShowProgress()
  ; Updates the progressbar to show a percentage complete as defined by 
  ; (the number of completed things that need doing) over (the total number of things that need doing) 
  ; multiplied by 100 to give a percentage.
  ; It also increments the number of completed functions.

   $NumberOfCompletedFunctionCalls += 1
   $i = ($NumberOfCompletedFunctionCalls / $TotalNumberOfFunctionCalls) * 100
   ProgressSet( $i, $i & " percent complete.")

EndFunc

;;; Main;;;

ProgressOn("Progress Meter", "Doing Things", "0 percent")

DoAllTheseThings(); Do all the Things that need to be done before 'ThoseThings' can be done.
DoAllThoseThings(); Complete the Script by doing all the remaining Things.

ProgressSet(100 , "Done", "Complete")
sleep(500)
ProgressOff()

I dunno if that actually helps you... :( But I hope so.

Now, in say, 6 months, I might need to add another 'Thing' to be done in either or both of 'These' or 'Those'.

When I do that, I will need to manually change the $TotalNumberOfFunctionCalls.

I would like, if at all possible, for the Script itself to calculate that number.

(Or alternatively, a better way to do it... :oops: )

Link to comment
Share on other sites

  • Moderators

Ok, I'll have a look in a bit, but before I do a quick question (I have not read your comments yet), but how would the script know when it needs to change said value, what happens that would cause this?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Ok, I'll have a look in a bit, but before I do a quick question (I have not read your comments yet), but how would the script know when it needs to change said value, what happens that would cause this?

I really do appreciate this, just so you know! :)

The number of times the Function 'DoThisThing' is called (from within 'DoTheseThings') plus the number of times 'DoThatThing' is called (from within 'DoThoseThings').

The thing that would happen for this number to change, is someone (me or other) adding another line into the script. Basically copy/pasting DoThisThing($parameters) or DoThatThing($parameters).

The Parameters passed in every iteration of 'This' or 'That' are different.

Currently there are 15 'This' calls, and 5 'That' calls, all with different Parameters.

I expect this number to grow, and would also like to reuse the script for other things which will have there own number of 'This' and 'That'.

A simple copy/paste and changing the parameters is straightforward. But, having to manually change the total number of function calls is one of those little tasks that will get lost and forgotten, thus making the progress bar very inaccurate (if it even works when you get over 100%!!!). And so, if the script could tally the function calls within 'DoTheseThings' and 'DoThoseThings', I (we?) wouldn't have to worry about it.

Make sense?

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