5

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.
Community
  • 1
  • 1
Erric J Manderin
  • 1,096
  • 3
  • 10
  • 23

1 Answers1

7

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

Martijn van Put
  • 3,293
  • 18
  • 17
  • The Reactive Extensions seem very good; However could you or anybody show me how could I use it for my particular needs ? – Erric J Manderin Jul 27 '13 at 03:25
  • @ErricJManderin I've added 2 examples, how they help. – Martijn van Put Jul 27 '13 at 16:37
  • so what about if I wanted to resume or pause a task not just cancel it, or if I wanted to make sure that a specific task will never execute until another task has completed (Flow Like) – Erric J Manderin Jul 28 '13 at 00:15
  • For task flow you can use ContinueWith, i shall add an example. Pause doesn't really exists. You can cancel the task or wait for a task to complete but not pause and resume it. – Martijn van Put Jul 28 '13 at 07:24
  • I am so sorry for so much requests but The most important thing is to achieve that continuity in a dynamic way so that I can add more jobs in the run time or remove some... so on – Erric J Manderin Jul 28 '13 at 10:18
  • It is possible i think, if you remember the lastest task creation you can add the continuation task to it. – Martijn van Put Jul 28 '13 at 10:19
  • during the run time ?! – Erric J Manderin Jul 28 '13 at 10:21
  • Can you give me a bit more information about the situation. When do you want to add a continuation task after an other? Why not schedule it normally. – Martijn van Put Jul 28 '13 at 10:22
  • The application will create some requests to collect data from website so some times you need to get a token code that is needed for the other task to work and without it the request will be invalid... also in some other situations I have to load some data during program initialization and this data is depending on the latest run time saved data.... it is a very big situation – Erric J Manderin Jul 28 '13 at 10:25
  • So I want to centralize everything to run from a single Task Handler or manager that allows me to queue, run, schedule tasks with priorities and so on also have the abilities mentioned in the thread... Just for the sake of a more clean code – Erric J Manderin Jul 28 '13 at 10:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34319/discussion-between-martijn-van-put-and-erric-j-manderin) – Martijn van Put Jul 28 '13 at 10:27