Пример #1
0
        private static void Main(string[] args)
        {
            Log("Started.");
            try
            {
                using (WorldClock worldClock = new WorldClock())
                {
                    RunAsync(worldClock).Wait();
                }
            }
            catch (AggregateException e)
            {
                Log("*** " + e.InnerException.Message);
            }

            Log("Completed.");
        }
Пример #2
0
        private static async Task RunAsync(WorldClock worldClock)
        {
            DateTime initialTime = await worldClock.GetTimeAsync();

            Log("Time is " + TimeString(initialTime));

            // An infinite loop of successive 100 ms delay tasks
            Func <Task> delayAsync = delegate
            {
                Log("Delaying...");
                return(Task.Delay(TimeSpan.FromSeconds(0.1d)));
            };

            LoopingScheduler scheduler = new LoopingScheduler(delayAsync);

            Func <DateTime, TimeSpan, Task> throwIfMaxDurationAsync = async delegate(DateTime start, TimeSpan duration)
            {
                Log("Checking time...");
                DateTime now = await worldClock.GetTimeAsync();

                Log("Time is " + TimeString(now));
                if ((now - start) > duration)
                {
                    throw new InvalidOperationException("Max duration reached!");
                }
            };

            // On every pause cycle, check if duration is reached based on world time
            scheduler.Paused += delegate(object sender, AsyncEventArgs e)
            {
                Log("Paused.");
                e.Tasks.Add(() => throwIfMaxDurationAsync(initialTime, TimeSpan.FromSeconds(2.0d)));
            };

            TimeSpan pauseInterval = TimeSpan.FromSeconds(0.5d);
            await scheduler.RunAsync(pauseInterval);
        }