Forum C# Chaining functions in ordered sequence

Chaining functions in ordered sequence

Postby jasonMcIntosh » June 6th, 2011, 6:11 pm

I'm not thoroughly familiar with C# yet, so maybe there's an obvious answer to this, but...

In light of the function call time limit of 100ms, what is a best way to chain a sequence of calls to complete some functionality that may require longer? (Ie, iterating and processing a list of objects, initialization code, etc.)

Some kind of "call in the future" scheduler would seem ideal, but I don't know if such a thing exists in C# or if I need to build it.

Thanks for any help!
Jason McIntosh
Otherwhere Gameworks
jasonMcIntosh
 
Posts: 81
Joined: February 25th, 2011, 4:51 am

Re: Chaining functions in ordered sequence

Postby jasonMcIntosh » June 7th, 2011, 4:28 am

Seems like Timer is the best option for this. I just want to make sure that the function called will not be part of the current thread's execution time.
Jason McIntosh
Otherwhere Gameworks
jasonMcIntosh
 
Posts: 81
Joined: February 25th, 2011, 4:51 am

Re: Chaining functions in ordered sequence

Postby Henrik » June 7th, 2011, 11:47 am

I'm pretty sure you're not allowed to use that timer, but there are timers built-in to Player.IO that you should use instead:

http://playerio.com/documentation/refer ... leCallback
http://playerio.com/documentation/refer ... e#AddTimer

In your case you should use ScheduleCallback to chain calls.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Chaining functions in ordered sequence

Postby jasonMcIntosh » June 7th, 2011, 5:34 pm

Aha! Perfect! Thanks, Henrik.
Jason McIntosh
Otherwhere Gameworks
jasonMcIntosh
 
Posts: 81
Joined: February 25th, 2011, 4:51 am

Re: Chaining functions in ordered sequence

Postby jasonMcIntosh » June 7th, 2011, 11:34 pm

So ScheduleCallback() doesn't allow you to call a function which takes arguments?

I'm still trying to figure out how delegates work, so maybe I'm just confused, but it looks like Action<> allows a single argument, while Action doesn't allow any arguments. Is that correct? If so, that's quite an unfortunate constraint.

And you're sure Timer can't be white listed? :)

(After all, ScheduleCallback() returns an instance of Timer.)
Jason McIntosh
Otherwhere Gameworks
jasonMcIntosh
 
Posts: 81
Joined: February 25th, 2011, 4:51 am

Re: Chaining functions in ordered sequence

Postby jasonMcIntosh » June 7th, 2011, 11:41 pm

...And if we can't get Timer white listed, can we get an overloaded ScheduleCallback() which takes Action<T>? I could at least then pass an array of values as parameters for more flexibility.
Jason McIntosh
Otherwhere Gameworks
jasonMcIntosh
 
Posts: 81
Joined: February 25th, 2011, 4:51 am

Re: Chaining functions in ordered sequence

Postby Henrik » June 8th, 2011, 8:05 am

jasonMcIntosh wrote:So ScheduleCallback() doesn't allow you to call a function which takes arguments?

I'm still trying to figure out how delegates work, so maybe I'm just confused

If a method takes an argument of the type Action<T>, i.e. a method which takes one argument of the type T, then that first method will supply the arguments. For example, on the type List<T>, there's a method called ForEach(Action<T> action). So if I have a List<string>, and want to call ForEach, I need to supply a delegate, a method, that takes exactly one string argument, but I'm not the one calling this method, ForEach will do it, for each (duurr) of the strings in my list. I don't supply the arguments, ForEach does that.

So the ScheduleCallback method takes a simple Action, without arguments, because ScheduleCallback doesn't have any arguments to pass in. It just takes a piece of code and executes that, and if that code contains other method calls, well then you have to supply the arguments for those inline, which works fine because you're in the right scope. Like this:

Code: Select all
private void process(List<SomeThing> batch, int size) {
   if (batch.Count <= size) {
      process(batch);
   } else {
      process(batch.GetRange(0, size));
      this.ScheduleCallback(delegate() {
         process(batch.GetRange(size, batch.Count - size), size);
      }, 10);
   }
}

private void process(List<SomeThing> batch) {
   //Do something with the batch.
}


The above code will go through your list of things, process size items, and if there are any remaining, schedule a callback to process the remainder recursively.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Chaining functions in ordered sequence

Postby jasonMcIntosh » June 8th, 2011, 5:22 pm

Rock on, Henrik! 8-)

Thank you extra bunches for taking the time to explain with such detail. Your example has clarified much.
Jason McIntosh
Otherwhere Gameworks
jasonMcIntosh
 
Posts: 81
Joined: February 25th, 2011, 4:51 am


Return to C#