Erric J Manderin
Erric J Manderin

Reputation: 1096

Reliable Task 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

Upvotes: 5

Views: 1089

Answers (1)

Martijn van Put
Martijn van Put

Reputation: 3313

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

Upvotes: 7

Related Questions