Dreamine.Logging.Wpf 1.0.2
Dreamine.Logging.Wpf 사용자 인터페이스 기능과 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
BatchedDispatcher.cs
이 파일의 문서화 페이지로 가기
1using System;
2using System.Collections.Concurrent;
3using System.Collections.Generic;
4using System.Threading;
5using System.Windows.Threading;
6
8{
33 public sealed class BatchedDispatcher<T>
34 {
43 public const int MaxBatchSize = 256;
44
53 private readonly Dispatcher _dispatcher;
62 private readonly Action<IReadOnlyList<T>> _onBatch;
71 private readonly DispatcherPriority _priority;
80 private readonly ConcurrentQueue<T> _pending = new();
89 private int _scheduled;
90
132 Dispatcher dispatcher,
133 Action<IReadOnlyList<T>> onBatch,
134 DispatcherPriority priority = DispatcherPriority.Background)
135 {
136 _dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
137 _onBatch = onBatch ?? throw new ArgumentNullException(nameof(onBatch));
138 _priority = priority;
139 }
140
157 public void Enqueue(T item)
158 {
159 _pending.Enqueue(item);
161 }
162
172 {
173 // Ensure only one DispatcherOperation is pending at any time.
174 if (Interlocked.CompareExchange(ref _scheduled, 1, 0) != 0)
175 {
176 return;
177 }
178
179 _dispatcher.BeginInvoke(_priority, new Action(Flush));
180 }
181
190 private void Flush()
191 {
192 try
193 {
194 var buffer = new List<T>(Math.Min(_pending.Count, MaxBatchSize));
195
196 while (buffer.Count < MaxBatchSize && _pending.TryDequeue(out var item))
197 {
198 buffer.Add(item);
199 }
200
201 if (buffer.Count > 0)
202 {
203 _onBatch(buffer);
204 }
205 }
206 finally
207 {
208 Volatile.Write(ref _scheduled, 0);
209
210 // Re-arm if producers added more while we were running.
211 if (!_pending.IsEmpty)
212 {
214 }
215 }
216 }
217 }
218}
BatchedDispatcher(Dispatcher dispatcher, Action< IReadOnlyList< T > > onBatch, DispatcherPriority priority=DispatcherPriority.Background)
readonly Action< IReadOnlyList< T > > _onBatch