C# – Continuous task example

    public interface IContinueTask
    {
        string TaskName { get; }
        TimeSpan Interval { get; }
        IObservable<TimeSpan> ObservableIntervalChange { get; }
        void Action();
    }

    public class ContinuousTaskItem
    {
        private CancellationTokenSource m_cancelTokenSource = new CancellationTokenSource();
        private IContinueTask m_task;

        public ContinuousTaskItem(IContinueTask task)
        {
            m_task = task;
        }

        public bool Cancel()
        {
            m_cancelTokenSource.Cancel();
            return true;
        }

        public Task Create()
        {
            return Task.Factory.StartNew(
                () =>
                {
                    while (true)
                    {
                        if (m_cancelTokenSource.Token.WaitHandle.WaitOne(m_task.Interval))
                            break;

                        m_task.Action();
                    }
                }, m_cancelTokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
        }
    }

    public class ContinuousTaskManager
    {
        private Dictionary<string, ContinuousTaskItem> m_taskList = new Dictionary<string, ContinuousTaskItem>();

        public ContinuousTaskManager()
        {

        }

        public bool AddContinueTask(IContinueTask task)
        {
            lock (m_taskList)
            {
                if (m_taskList.Keys.FirstOrDefault(x => x == task.TaskName) == null)
                {
                    var ob = task.ObservableIntervalChange.Subscribe(
                            interval =>
                            {
                                lock (m_taskList)
                                {
                                    // delete task
                                    ContinuousTaskItem taskItem = null;
                                    if (m_taskList.TryGetValue(task.TaskName, out taskItem))
                                    {
                                        taskItem.Cancel();
                                        m_taskList.Remove(task.TaskName);

                                        // create again
                                        var newTask = new ContinuousTaskItem(task);
                                        m_taskList.Add(task.TaskName, newTask);
                                        newTask.Create();
                                    }
                                }
                            }
                        );
                    var item = new ContinuousTaskItem(task);
                    m_taskList.Add(task.TaskName, item);
                    item.Create();

                    return true;
                }
                return false;
            }
        }

        public bool RemoveTask(string taskName)
        {
            lock (m_taskList)
            {
                // delete task
                ContinuousTaskItem taskItem = null;
                if (m_taskList.TryGetValue(taskName, out taskItem))
                {
                    taskItem.Cancel();
                    m_taskList.Remove(taskName);
                    return true;
                }
            }
            return false;
        }

        public void Clear()
        {
            lock (m_taskList)
            {
                foreach (var task in m_taskList.Values)
                {
                    task.Cancel();
                }

                m_taskList.Clear();
            }
        }
    }
Advertisements
This entry was posted in .NET, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s