none
Round Robin Scheduler in C#

    Question

  • Hi Folks,

    Looking for some guidance on this.

    I would like to create a function that switches between a series of tasks.

    Something like this:

    void task_switch()

    {

    switch(task)

    {

    case task1 :

    {

    task_1_function();

    task = task2;

    break;

    }

    case task2 :

    {

    task_2_function();

    task = task3;

    break;

    }

    case task3 :

    etc......................

    }

    I am from the embedded world and have done this successfully using interrupt service routines in C.

    Does Visual C#.net offer something like an ISR? Is there some simple way i can repeatedly call the task_switch() function in order to skip through each of the task_x_functions() ??

    Thanks in advance. 

    Ade

    Friday, October 12, 2012 2:58 PM

Answers

  • Using the switch is a pretty poor idea as we can find a cleaner OO approach here:

    I would consider using a ring buffer containing the tasks. Otherwise use a List<TBaseTask> and an index variable to mimic a simple ring buffer.

    Friday, October 12, 2012 3:07 PM
  • Here is a simple state machine using clases.  I made the class a List<> object and double linked the classes.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class task
        {
            public task nextTask;
            public task previousTask;
            public string name;
        }
        
        class Program
        {
            static List<task> tasks = new List<task>();
            static void Main(string[] args)
            {
                const int NUMBER_TASKS = 10;
                task previousTask = null;
                for (int index = 0; index < NUMBER_TASKS; index++)
                {
                    task newTask = new task();
                    newTask.name = "Task : " + index.ToString();
                    newTask.previousTask = previousTask;
                    if (index != 0)
                        previousTask.nextTask = newTask;
                    previousTask = newTask;
                    tasks.Add(newTask);
                }
                previousTask.nextTask = null;
            }
        }
    }


    jdweng

    Tuesday, October 16, 2012 4:15 PM
  • I would simply use a Queue:

    Queue<Task> queue = new Queue<Task>();
    //...enqueue tasks
    
    while (true)
    {
        Task current = queue.Dequeue();
    
        //use current task
    
        //if task isn't finished
        queue.Enqueue(current);
    }

    Tuesday, October 16, 2012 4:40 PM

All replies

  • Using the switch is a pretty poor idea as we can find a cleaner OO approach here:

    I would consider using a ring buffer containing the tasks. Otherwise use a List<TBaseTask> and an index variable to mimic a simple ring buffer.

    Friday, October 12, 2012 3:07 PM
  • thanks for the feedback Stefan.

    I will look into your suggestions. 

    Tuesday, October 16, 2012 2:39 PM
  • Here is a simple state machine using clases.  I made the class a List<> object and double linked the classes.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class task
        {
            public task nextTask;
            public task previousTask;
            public string name;
        }
        
        class Program
        {
            static List<task> tasks = new List<task>();
            static void Main(string[] args)
            {
                const int NUMBER_TASKS = 10;
                task previousTask = null;
                for (int index = 0; index < NUMBER_TASKS; index++)
                {
                    task newTask = new task();
                    newTask.name = "Task : " + index.ToString();
                    newTask.previousTask = previousTask;
                    if (index != 0)
                        previousTask.nextTask = newTask;
                    previousTask = newTask;
                    tasks.Add(newTask);
                }
                previousTask.nextTask = null;
            }
        }
    }


    jdweng

    Tuesday, October 16, 2012 4:15 PM
  • I would simply use a Queue:

    Queue<Task> queue = new Queue<Task>();
    //...enqueue tasks
    
    while (true)
    {
        Task current = queue.Dequeue();
    
        //use current task
    
        //if task isn't finished
        queue.Enqueue(current);
    }

    Tuesday, October 16, 2012 4:40 PM
  • thanks folks. I think the List<> and Queue<> approach are easier for me to get my head around.

    I will have a tinker and re-post if required.

    Thanks again for the advice. 

    Ade. 

     
    Thursday, October 18, 2012 8:17 AM
  • Does Visual C#.net offer something like an ISR? -

    In .NET and in Windows's user mode you have no permission to communicate with devices directly, and sure you are not permitted to get interrupts and handle them. If you need such possibility the only way is to write you SW executed in kernel mode (driver) just only using pure C.

    Thursday, October 18, 2012 2:44 PM