예제 #1
0
        static async Task StartDemo(CancellationToken mainToken, SingleScheduler scheduler)
        {
            Console.WriteLine("About to try to schedule work ");

            var until     = TimeSpan.FromSeconds(SecondsForScheduledWorkToBeCarriedOut);
            var scheduled = scheduler.ScheduleWork(mainToken, until, 30);

            if (scheduled)
            {
                Console.WriteLine($"Scheduled task completed");
            }
            else
            {
                Console.WriteLine("Waiting before trying to get a lock again");

                try
                {
                    await Task.Delay(DelayInSecondsBeforeSchedulingAgain *1000, mainToken);
                }
                catch (TaskCanceledException)
                {
                    Console.WriteLine("blocking delay in progres when task was cancelled");
                }
            }
        }
예제 #2
0
        static void SetupDemo(string counterPath, string outPath)
        {
            var lockPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(counterPath), FileLock);

            var duration = TimeSpan.FromMinutes(TotalMinutesDuration);
            var runUntil = DateTime.Now.Add(duration);

            var workerFactory   = new WorkerFactory(System.Diagnostics.Process.GetCurrentProcess().Id, counterPath, outPath);
            var singleScheduler = new SingleScheduler(workerFactory.Create, lockPath);

            using (var mainTokenSource = new CancellationTokenSource(duration.Add(TimeSpan.FromSeconds(SecondsLeewayForCancellationSource))))
            {
                try
                {
                    var mainTask = Task.Run(async() =>
                    {
                        Console.WriteLine("Starting Main Processing");

                        while (DateTime.Now < runUntil)
                        {
                            await StartDemo(mainTokenSource.Token, singleScheduler);
                        }
                    }, mainTokenSource.Token);

                    mainTask.Wait();
                }
                catch (OperationCanceledException)
                {
                    if (mainTokenSource.IsCancellationRequested)
                    {
                        Console.WriteLine("Main process cancelled in main loop");
                    }
                }
            }
        }