private void Start() { //An action to send out workers, is scheduled for periodic execution //This is an alternative to implementing the ILoadBalanced interface var action = new RepeatableAction((ignored) => { SendWorkers(); return(true); }); LoadBalancer.defaultBalancer.Add(action, 0.3f, true); }
private void Update() { if (Input.GetKeyUp(KeyCode.Alpha1)) { _counter = 0; //Here we schedule some work to repeat 4 times, i.e. it will execute a total of 5 times. var action = new RepeatableAction(DoWork, 4); //Here we schedule the action to start as soon as possible and repeat at the default interval of the load balancer. ExamplesLoadBalancer.examplesBalancer.Add(action); } else if (Input.GetKeyUp(KeyCode.Alpha2)) { //Here we create an action using an anonymous method. We also do not specify a repetition count, instead we control the life time in the method. int counter = 0; var action = new RepeatableAction( (t) => { Debug.Log("Execution " + ++counter + " done after " + t.ToString() + " seconds"); return(counter < 5); }); //Here we schedule the action to take place after a short delay of 3 seconds or as soon after that delay as possible. //It will then repeat with an interval of 2 seconds. ExamplesLoadBalancer.examplesBalancer.Add(action, 2f, 3f); } else if (Input.GetKeyUp(KeyCode.Alpha3)) { //Here we create an action using an anonymous method. We also do not specify a repetition count, and instruct it to continue indefinitely. //The only way to stop it is to use the handle returned from the load balancer when the action is added. int counter = 0; var action = new RepeatableAction( (t) => { Debug.Log("Execution " + ++counter + " done after " + t.ToString() + " seconds"); return(true); }); //Here we schedule the action to take as soon as possible. //It will then repeat with an interval of 2 seconds. _handle = ExamplesLoadBalancer.examplesBalancer.Add(action, 2f); } else if (Input.GetKeyUp(KeyCode.Alpha4)) { //Here we stop the action initiated in 3 above if (_handle != null) { _handle.Stop(); } } }
/// <summary> /// Repeats <paramref name="action"/> a number of times. Functionally similar to a <c>for</c> loop. /// The number of times the action is repeated is determined by subtracting <paramref name="iterationStartIndex"/> /// from <paramref name="iterations"/>. /// </summary> /// <param name="iterationStartIndex">Start index of the iteration.</param> /// <param name="iterations">The number iterations.</param> /// <param name="action">The action to repeat.</param> public static void Repeat(int iterationStartIndex, int iterationEndIndex, RepeatableAction action) { for (int i = iterationStartIndex; i < iterationEndIndex; i++) { action(i); } }