-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEiiiTask.cs
More file actions
196 lines (167 loc) · 3.92 KB
/
EiiiTask.cs
File metadata and controls
196 lines (167 loc) · 3.92 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace EventImplementedInduceInterface
{
public class EiiiTask<T>
{
#region Fields
private const int TIM_TSK_STRIDE = 100;
protected Task task;
protected string starter;
protected TaskToCallback cb;
protected int frequency;
protected TaskLifeSpan life;
private List<ILauncher<T>> launcher;
private CancellationTokenSource cancelsource;
private CancellationTokenSource cancelsourceclients;
#endregion Fields
#region Event
public void Subscribe(ILauncher<T> lchr)
{
//pub.Raised += OnEvent;
lchr.Raise += new EventHandler<object, EiiiEventArgs<T>>(OnEvent);
this.launcher.Add(lchr);
}
internal virtual void OnEvent(object sender, EiiiEventArgs<T> e)
{
Debug.WriteLine("OnEvent(" + DateTime.Now.ToString() + ")" + e.ToString());
eventFunc(sender, e);
}
/// <summary>
/// イベント関数 virtual
/// </summary>
internal virtual void eventFunc<T>(object sender, EiiiEventArgs<T> e)
{
// override してここに目的の処理を記述
}
async internal void start()
{
await Start();
}
#endregion Event
#region Cancellation
protected void OnTaskCancelerInitialize()
{
initCancelResouces();
}
private void initCancelResouces()
{
if (cancelsource == null)
cancelsource = new CancellationTokenSource();
if (cancelsource.IsCancellationRequested)
{
cancelsource.Dispose();
cancelsource = new CancellationTokenSource();
}
}
#endregion Cancellation
#region Constructor
/// <summary>
///
/// </summary>
public EiiiTask(TaskToCallback cb, CancellationTokenSource cs, int freq, int tlc)
{
this.launcher = new List<ILauncher<T>>();
this.cb = cb;
this.cancelsource = cs;
initCancelResouces();
this.frequency = (freq > 0) ? freq : 100;
this.life = (tlc != 0) ? TaskLifeSpan.Infinite : TaskLifeSpan.Single;
this.starter = null;
}
#endregion Constructor
#region Externals
public async Task<string> Start()
{
if (this.starter != null)
return null;
if (startOptions() == null)
return null;
await Task.Delay(0);
this.starter = await starterTask(cancelsource.Token);
return this.starter;
}
/// <summary>
/// タスクを再起動する
/// </summary>
internal virtual Task<int> restartTask()
{
Task.Run(async () =>
{
cancelsource.Cancel();
await Task.Delay(TIM_TSK_STRIDE * 2);
await Task.Delay(1000);
var lifebak = this.life;
this.life = TaskLifeSpan.Single;
this.starter = null;
this.life = lifebak;
start();
});
return Task.FromResult(0);
}
protected virtual string startOptions()
{
// additional coe here
return "";
}
public TaskStatus GetStatus()
{
if (this.task == null)
return TaskStatus.Faulted;
return this.task.Status;
}
#endregion Externals
#region Tasks
internal virtual Task<string> starterTask(CancellationToken ct)
{
// override ere
var res = "";
Task.Run(async () =>
{
this.task = await mainTask(ct);
this.starter = null;
});
return Task.FromResult<string>(res);
}
internal virtual async Task<Task<int>> mainTask(CancellationToken ct)
{
// override here
int t = 0;
while (this.life == TaskLifeSpan.Infinite)
{
try
{
await Task.Delay(TIM_TSK_STRIDE);
// キャンセル待機可
ct.ThrowIfCancellationRequested();
t++;
mainFunc(t);
}
catch (Exception ex)
{
// キャンセル
Debug.WriteLine(ex.Message);
if (ct.IsCancellationRequested == true)
OnTaskCancelerInitialize();
else
{
throw new Exception("throw at ", ex);
}
return Task.FromResult<int>(t);
}
}
return Task.FromResult<int>(t);
}
/// <summary>
/// Task to
/// </summary>
internal virtual void mainFunc(int t)
{
this.cb(t.ToString());
}
#endregion Tasks
}
}