Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reliable Task Schedule

Tags:

c#

schedule

Well... Please excuse me for positing such vague question but i am crashing my head because of it and i can't find a good logic to implement it or at least a good library that do such thing for me.

Situation

My application should be executing a lot of tasks in a different time intervals, some of which needs to be executed only after some conditions are satisfied or other methods completed and so on. [ think of it as a method dependency tree]... And i was wondering in such big projects like a Huge online game or such projects, how they organize their code in order to not crash or execute some methods in a wrong time or without satisfying it's conditions ?

Problem

The whole problem is that in my application i want the following specs

  • Ability to schedule a method to run at a specified time.
  • Ability to pause, cancel, stop, or even repeat a task.
  • Ability to not execute a specific task until another task is finished so i can create some kind of Flow.
  • Ability to create some kind of Flow in order to make sure that some methods will never execute until it's parent or procedure method have finished.
  • All that in an organized, fluent yet powerful way.
like image 925
Erric J Manderin Avatar asked Jul 26 '13 19:07

Erric J Manderin


People also ask

What is task scheduling explain with example?

The Task Scheduler is a tool included with Windows that allows predefined actions to be automatically executed whenever a certain set of conditions is met. For example, you can schedule a task to run a backup script every night, or send you an e-mail whenever a certain system event occurs.

What is task scheduling system?

A task scheduling software is a program that aids in scheduling all of your work, tracking your team's availability, assigning the correct resources to the task at hand, and keeping your projects on track.


1 Answers

Reactive Extensions (Rx.NET) might do the job! http://msdn.microsoft.com/en-us/data/gg577609.aspx

Examples:

This examples schedules a task execution.

Console.WriteLine("Current time: {0}", DateTime.Now);

// Start event 30 seconds from now.
IObservable<long> observable = Observable.Timer(TimeSpan.FromSeconds(30));

// Token for cancelation
CancellationTokenSource source = new CancellationTokenSource();

// Create task to execute.
Task task = new Task(() => Console.WriteLine("Action started at: {0}", DateTime.Now));

// Subscribe the obserable to the task on execution.
observable.Subscribe(x => task.Start(), source.Token);

// If you want to cancel the task do: 
//source.Cancel();

 Console.WriteLine("Press any key to exit");
 Console.ReadKey();

Result: enter image description here

Example 2:

Repeating a task every x seconds.

Console.WriteLine("Current time: {0}", DateTime.Now);

// Repeat every 2 seconds.
IObservable<long> observable = Observable.Interval(TimeSpan.FromSeconds(2));

// Token for cancelation
CancellationTokenSource source = new CancellationTokenSource();

// Create task to execute.
Action action = (() => Console.WriteLine("Action started at: {0}", DateTime.Now));

// Subscribe the obserable to the task on execution.
observable.Subscribe(x => { Task task = new Task(action);task.Start(); },source.Token);

// If you want to cancel the task do: 
//source.Cancel();
Console.WriteLine("Press any key to exit");
Console.ReadKey();

Result: enter image description here

Example task continue:

Console.WriteLine("Current time: {0}", DateTime.Now);

        // Repeat every 2 seconds.
        IObservable<long> observable = Observable.Interval(TimeSpan.FromSeconds(2));

        // Token for cancelation
        CancellationTokenSource source = new CancellationTokenSource();

        // Create task to execute.
        Action action = (() => Console.WriteLine("Action started at: {0}", DateTime.Now));
        Action resumeAction = (() => Console.WriteLine("Second action started at {0}", DateTime.Now));

        // Subscribe the obserable to the task on execution.
        observable.Subscribe(x => { Task task = new Task(action); task.Start();
                                      task.ContinueWith(c => resumeAction());
        }, source.Token);

        // If you want to cancel the task do: 
        //source.Cancel();
        Console.WriteLine("Press any key to exit");
        Console.ReadKey();

Result:

enter image description here

like image 108
Martijn van Put Avatar answered Sep 17 '22 04:09

Martijn van Put