Ejemplo n.º 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            int    times       = int.Parse(InputBox.Ask("Times?", "3"));
            int    delay       = int.Parse(InputBox.Ask("Delay ms?", "1000"));
            double successRate = double.Parse(InputBox.Ask("Success rate?", "0.5"));

            Random rnd = new Random();
            int    i   = 0;

            Log("Starting retry (times: {0}, delay: {1} ms, successRate: {2})",
                times, delay, successRate);
            JobScheduler.Retry(times, delay.Milliseconds(), () =>
            {
                try
                {
                    Log("Attempt: {0}", ++i);
                    if (rnd.NextDouble() < successRate)
                    {
                        Log("SUCCESS!");
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                catch
                {
                    Log("FAIL :(");
                    throw;
                }
            }).Then(() =>
            {
                Log("FINISHED!");
            });
        }
Ejemplo n.º 2
0
        private void button5_Click(object sender, EventArgs e)
        {
            int    minutes  = int.Parse(InputBox.Ask("Minutes?", "1"));
            Random rnd      = new Random();
            int    total    = 100;
            var    promises = Enumerable.Range(0, total).Select(index =>
            {
                Log("{0}) Starting...", index);
                int attempts = 0;
                var promise  = JobScheduler.Retry(3, minutes.Minutes(), () =>
                {
                    try
                    {
                        Log("{0}) Attempt: {1}", index, ++attempts);
                        var randomValue = rnd.NextDouble();
                        Thread.Sleep((int)(randomValue * 500));
                        if (randomValue < 0.05)
                        {
                            Log("{0}) SUCCESS!", index);
                            return(true);
                        }
                        else
                        {
                            throw new Exception();
                        }
                    }
                    catch
                    {
                        Log("{0}) FAIL :(", index);
                        throw;
                    }
                });
                promise.Then(val =>
                {
                    Log("{0}) VALUE: {1}", index, val);
                    if (val)
                    {
                        Thread.Sleep((int)(rnd.NextDouble() * 500));
                    }
                    else if (rnd.NextDouble() > 0.5)
                    {
                        throw new Exception("TU VIEJA!");
                    }
                });
                return(promise);
            });

            Promise.All(promises).Then(results =>
            {
                int successes = results.Count(each => each == true);
                int errors    = results.Count(each => each == false);
                Log("SUCCESSES: {0}, ERRORS: {1}", successes, errors);
            });
        }
Ejemplo n.º 3
0
        private void loopEveryButton_Click(object sender, EventArgs e)
        {
            int ms = int.Parse(InputBox.Ask("Milliseconds?", "1000"));

            int  i      = 0;
            char letter = letters[0];

            letters = letters.Skip(1).ToArray();
            JobScheduler.LoopEvery(ms.Milliseconds(), () =>
            {
                Log("{0}{0} - {1}", letter, ++i);
            });
        }
Ejemplo n.º 4
0
        private void inFutureButton_Click(object sender, EventArgs e)
        {
            int ms = int.Parse(InputBox.Ask("Milliseconds into the future?", "1000"));

            Log("Before!");
            Task.Run(() =>
            {
                var promise = JobScheduler
                              .ExecuteAfter(ms.Milliseconds(), () => Log("Future!"))
                              .Then(() => Log("FINISHED 1!"))
                              .Then(() => Log("FINISHED 2!"));
                Log("After!");
                Task.Delay(ms * 3).Wait();
                promise.Then(() => Log("FINISHED 3!"));
            });
        }
Ejemplo n.º 5
0
        private void inFutureReturnButton_Click(object sender, EventArgs e)
        {
            int ms = int.Parse(InputBox.Ask("Milliseconds into the future?", "1000"));

            Log("Before!");
            var promise = JobScheduler
                          .ExecuteAfter(ms.Milliseconds(), () =>
            {
                Log("Future!");
                return(Environment.TickCount);
            })
                          .Then(ign => Log("FINISHED 1!"))
                          .Then(ign => Log("FINISHED 2!"));

            Log("After!");
            Thread.Sleep(ms * 2);
            promise.Then(ticks => Log("FINISHED 3! {0}", ticks));
        }
Ejemplo n.º 6
0
 private void testInputBoxButton_Click(object sender, EventArgs e)
 {
     MessageBox.Show("Yo soy: " + InputBox.Ask("Quién soy?"));
 }