Skip to content

Commit 67b6e61

Browse files
authored
Merge pull request #33 from vonhoff/v3.1.2
v3.1.2
2 parents a7c24e6 + a170928 commit 67b6e61

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

CHANGES.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
## Release Notes - Serilog.Sinks.RichTextBox.WinForms.Colored v3.1.1
1+
## Release Notes - Serilog.Sinks.RichTextBox.WinForms.Colored v3.1.2
22

33
### What Changed
44

5-
- Fixed a bug where the TextFormatter would crash when the value or format was null or empty.
5+
- Fixed a bug where cross-thread log messages during form construction caused `System.InvalidOperationException` due to premature access of the RichTextBox.
6+
7+
### Contributors
8+
9+
- [Steffen S. Hellestøl](https://github.com/SteffenSH)
610

711
### Resources
812

Serilog.Sinks.RichTextBox.WinForms.Colored/Serilog.Sinks.RichTextBox.WinForms.Colored.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
<TargetFrameworks>net462;net471;net6.0-windows;net8.0-windows;net9.0-windows;netcoreapp3.0-windows;netcoreapp3.1-windows</TargetFrameworks>
2323
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2424
<UseWindowsForms>true</UseWindowsForms>
25-
<Version>3.1.1</Version>
25+
<Version>3.1.2</Version>
2626
<PackageReleaseNotes>
27-
- Fixed a bug where the TextFormatter would crash when the value or format was null or empty.
27+
- Fixed a bug where cross-thread log messages during form construction caused `System.InvalidOperationException` due to premature access of the RichTextBox.
2828

29-
See repository for more information:
30-
https://github.com/vonhoff/Serilog.Sinks.RichTextBox.WinForms.Colored
29+
See repository for more information:
30+
https://github.com/vonhoff/Serilog.Sinks.RichTextBox.WinForms.Colored
3131
</PackageReleaseNotes>
3232
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
3333
<SupportedOSPlatform>windows</SupportedOSPlatform>

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/Extensions/RichTextBoxExtensions.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Runtime.InteropServices;
21+
using System.Threading;
2122
using System.Windows.Forms;
2223

2324
namespace Serilog.Sinks.RichTextBoxForms.Extensions
@@ -54,8 +55,25 @@ private struct Point
5455
private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, ref Point lParam);
5556
#endif
5657

57-
public static void SetRtf(this RichTextBox richTextBox, string rtf, bool autoScroll)
58+
public static void SetRtf(this RichTextBox richTextBox, string rtf, bool autoScroll, CancellationToken token)
5859
{
60+
//Wait for richTextBox.Handle to be created
61+
if (!richTextBox.IsHandleCreated)
62+
{
63+
var mre = new ManualResetEventSlim();
64+
EventHandler eh = (sender, args) => mre.Set();
65+
richTextBox.HandleCreated += eh;
66+
try
67+
{
68+
if (richTextBox.IsHandleCreated) mre.Set();
69+
mre.Wait(token);
70+
}
71+
finally
72+
{
73+
richTextBox.HandleCreated -= eh;
74+
}
75+
}
76+
5977
if (richTextBox.InvokeRequired)
6078
{
6179
richTextBox.BeginInvoke(new Action(() => SetRtfInternal(richTextBox, rtf, autoScroll)));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private void ProcessMessages(CancellationToken token)
133133
{
134134
_renderer.Render(evt, builder);
135135
}
136-
_richTextBox.SetRtf(builder.Rtf, _options.AutoScroll);
136+
_richTextBox.SetRtf(builder.Rtf, _options.AutoScroll, token);
137137
lastFlush = DateTime.UtcNow;
138138
}
139139
}

0 commit comments

Comments
 (0)