Skip to content

Commit e79e5f3

Browse files
committed
Update RichTextBoxSink.cs
1 parent 1caf6fe commit e79e5f3

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/RichTextBoxSink.cs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,35 +88,37 @@ private void ProcessMessages(CancellationToken token)
8888
{
8989
var builder = new RtfBuilder(_options.Theme);
9090
var snapshot = new System.Collections.Generic.List<LogEvent>(_options.MaxLogLines);
91-
var lastFlush = DateTime.UtcNow;
9291
var flushInterval = TimeSpan.FromMilliseconds(FlushIntervalMs);
92+
var lastFlush = DateTime.UtcNow;
9393

9494
while (!token.IsCancellationRequested)
9595
{
96-
_signal.WaitOne(flushInterval);
97-
if (Interlocked.CompareExchange(ref _hasNewMessages, 1, 1) == 0)
98-
{
99-
continue;
100-
}
96+
_signal.WaitOne();
10197

102-
var now = DateTime.UtcNow;
103-
var elapsed = now - lastFlush;
104-
if (elapsed < flushInterval)
98+
if (Interlocked.CompareExchange(ref _hasNewMessages, 0, 1) == 1)
10599
{
106-
continue;
100+
var now = DateTime.UtcNow;
101+
var elapsed = now - lastFlush;
102+
if (elapsed < flushInterval)
103+
{
104+
// Wait for the remaining time, or until a new message arrives
105+
var remaining = flushInterval - elapsed;
106+
if (remaining > TimeSpan.Zero)
107+
{
108+
_signal.WaitOne(remaining);
109+
}
110+
}
111+
112+
// Take a snapshot and flush
113+
_buffer.TakeSnapshot(snapshot);
114+
builder.Clear();
115+
foreach (var evt in snapshot)
116+
{
117+
_renderer.Render(evt, builder);
118+
}
119+
_richTextBox.SetRtf(builder.Rtf, _options.AutoScroll);
120+
lastFlush = DateTime.UtcNow;
107121
}
108-
109-
_buffer.TakeSnapshot(snapshot);
110-
builder.Clear();
111-
foreach (var evt in snapshot)
112-
{
113-
_renderer.Render(evt, builder);
114-
}
115-
116-
_richTextBox.SetRtf(builder.Rtf, _options.AutoScroll);
117-
Interlocked.Exchange(ref _hasNewMessages, 0);
118-
119-
lastFlush = now;
120122
}
121123
}
122124
}

0 commit comments

Comments
 (0)