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.
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 ?
The whole problem is that in my application i want the following specs
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.
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.
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:
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:
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With