Dreamine.Threading 1.0.1
이 패키지는 .NET ThreadPool이나 Task Parallel Library를 대체하려는 목적이 아닙니다.
로딩중...
검색중...
일치하는것 없음
DreamineThreadJob.cs
이 파일의 문서화 페이지로 가기
3
5
15{
24 private readonly Func<CancellationToken, ValueTask> _action;
33 private DateTimeOffset? _lastExecutedAt;
42 private long _executeCount;
43
52 public string Name { get; }
53
63
72 public long ExecuteCount => Interlocked.Read(ref _executeCount);
73
82 public DateTimeOffset? LastExecutedAt => _lastExecutedAt;
83
92 public Exception? LastException { get; private set; }
93
128 Func<CancellationToken, ValueTask> action)
129 {
130 ArgumentNullException.ThrowIfNull(options);
131 ArgumentNullException.ThrowIfNull(action);
132
133 Options = options.Normalize();
134 Name = Options.Name;
135 _action = action;
136 }
137
162 public bool ShouldRun(DateTimeOffset now)
163 {
164 if (!Options.IsEnabled)
165 {
166 return false;
167 }
168
169 if (Options.IntervalMs == 0)
170 {
171 return true;
172 }
173
174 if (_lastExecutedAt is null)
175 {
176 return true;
177 }
178
179 var elapsedMs = (now - _lastExecutedAt.Value).TotalMilliseconds;
180 return elapsedMs >= Options.IntervalMs;
181 }
182
223 public async ValueTask ExecuteAsync(CancellationToken cancellationToken)
224 {
225 try
226 {
227 await _action(cancellationToken).ConfigureAwait(false);
228
229 _lastExecutedAt = DateTimeOffset.Now;
230 Interlocked.Increment(ref _executeCount);
231 LastException = null;
232 }
233 catch (Exception ex) when (ex is not OperationCanceledException)
234 {
235 LastException = ex;
236 throw;
237 }
238 }
239}
readonly Func< CancellationToken, ValueTask > _action
DreamineThreadJob(DreamineThreadJobOptions options, Func< CancellationToken, ValueTask > action)
async ValueTask ExecuteAsync(CancellationToken cancellationToken)