C# - Continuous task example

1 minute read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    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();
            }
        }
    }