registerTimer has no Function! HELP!

Discuss, ask for help, share ideas, give suggestions, read tutorials, and tell us about bugs you have found with MicroMacro in here.

Do not post RoM-Bot stuff here. There is a subforum for that.
Forum rules
This is a sub-forum for things specific to MicroMacro.

This is not the place to ask questions about the RoM bot, which uses MicroMacro. There is a difference.
Post Reply
Message
Author
aliex
Posts: 12
Joined: Thu Feb 10, 2011 6:54 am

registerTimer has no Function! HELP!

#1 Post by aliex » Thu Feb 10, 2011 7:05 am

Im tyring to inclue a timer which calls a funtion every given time.
But it returns nothing, this is the code i got so far:

Code: Select all

needed = true;

function toggle()
	needed = true;
end

function main()
  registerTimer("timer", 5000, toggle);
  
  printf("Hello World!\n");
  printf("Press SPACE BAR to terminate script.\n");
 
  while(1) do
    -- if they press space bar, break out of the while
    if( keyPressedLocal(key.VK_SPACE) ) then
      break;
    end
	
	if( needed ) then
        	printf("needed!\n");
          	yrest (1000);
			needed = false;
          	yrest(1000);
    end
	
  end
 
  printf("Goodbye!\n");
 
end
 
 
-- startMacro will now call main() within a protected environment for us.
startMacro(main);
He should print the given line every 5 seconds, but he prints it only one time.

What am i doing wrong?

zuel
Posts: 21
Joined: Mon Jan 10, 2011 12:49 am

Re: registerTimer has no Function! HELP!

#2 Post by zuel » Thu Feb 10, 2011 11:02 am

Looks like your in a tight loop.

While(1)

...
End


Is not allowing any thread to process.

Make it

While(1)do

yrest(500);
end


you can remove yrest from your if statement.

So it looks like.

Code: Select all


while(1)do
   if( needed ) then
           printf("needed!\n");
           needed = false;
    end
   yrest(500);
end


User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: registerTimer has no Function! HELP!

#3 Post by Administrator » Thu Feb 10, 2011 11:40 am

Yeah, the timer processing will only be done while yielding. This is intentional (prevents potentially 'long' timer functions from interfering with time-critical tasks). Even a yrest(1) will do the trick (and prevent your script from using 100% of a core on the CPU).

zuel
Posts: 21
Joined: Mon Jan 10, 2011 12:49 am

Re: registerTimer has no Function! HELP!

#4 Post by zuel » Thu Feb 10, 2011 8:04 pm

I thought I read in the documentation that yrest(<100) is equivalent to rest(<100)

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: registerTimer has no Function! HELP!

#5 Post by Administrator » Thu Feb 10, 2011 8:14 pm

zuel wrote:I thought I read in the documentation that yrest(<100) is equivalent to rest(<100)
Just about. There has been a few changes to yrest since then that allow it to take advantage of certain features without a high rest value.

zuel
Posts: 21
Joined: Mon Jan 10, 2011 12:49 am

Re: registerTimer has no Function! HELP!

#6 Post by zuel » Thu Feb 10, 2011 11:30 pm

The document suggest any value less than 100 you might as well call rest. That is what I have been doing in my code. :)

Would you say then, that regardless of the rest period, always call yrest?

aliex
Posts: 12
Joined: Thu Feb 10, 2011 6:54 am

Re: registerTimer has no Function! HELP!

#7 Post by aliex » Fri Feb 11, 2011 12:52 am

Administrator wrote:Yeah, the timer processing will only be done while yielding. This is intentional (prevents potentially 'long' timer functions from interfering with time-critical tasks). Even a yrest(1) will do the trick (and prevent your script from using 100% of a core on the CPU).
guys thank you so much. works perfectly now! :)

now that i know what was missing, i want to understand WHY yrest() is so important. i get the point that i create some time, allowing the script to process other things.

how can i measure how much time i must set yrest? when and where in the script should it be inserted? what exactly means "yielding" ? (i'm german, my dictionary didnt help xD)

thanks again for your help.

zuel
Posts: 21
Joined: Mon Jan 10, 2011 12:49 am

Re: registerTimer has no Function! HELP!

#8 Post by zuel » Fri Feb 11, 2011 3:08 am

It's very simple.
Yielding is simply allowing something to go first.

Imagine two busy lanes in a road merging into one. If the drivers in the right lane never yielded for traffic in the left lane, the left lane would stop moving. Now imagine that every 10nth car in the right lane yielded for a car in the left lane. Only a tenth as many cars in the left lane would merge. The perfect scenario is that ever car in the right lane yielded to allow one car in the left lane ahead of them.

Multi-threading is the act of two separate sets of code trying to run at the same time on one processor.

Multi-threading principles are exactly the same. But instead of cars, we are talking lines of code and instead of lanes we are talking about the processor. Thereby, if you want to run two separate pieces of code utilizing one processor, each set of code needs to perform some amount of work and then yield for the other piece of code so it to can use the processor.

It's hard to say when you should yield. You have to ask yourself, how accurate do you want your timer. Do you need it to run EXACTLY on time? Then you need to add more yields. Or can you let it slip a second, then you can Yield less.

It's probably not wise to yield every other line. Just yield after your code has performed some amount of work.

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: registerTimer has no Function! HELP!

#9 Post by Administrator » Fri Feb 11, 2011 3:43 am

zuel wrote: Would you say then, that regardless of the rest period, always call yrest?
Unless you have a good reason to not yield at that time, then you should use yrest.

Code: Select all

how can i measure how much time i must set yrest? when and where in the script should it be inserted? what exactly means "yielding" ? (i'm german, my dictionary didnt help xD)
Just yield inside loops and such. That's the most important thing. You don't have to worry about timing it yourself (unless you specifically want to rest for 1 second or something like that). Just call yrest(1) to allow other coroutines a change to do their processing and they will return control back when they are done.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests