Skip to content

An experimental method for allowing a WinForms RichTextBox control to be asynchronous for the purpose of loading large files without blocking the UI thread.

License

Notifications You must be signed in to change notification settings

FenPhoenix/RichTextBoxAsync

Repository files navigation

RichTextBoxAsync

An experimental method for allowing a WinForms RichTextBox control to be asynchronous for the purpose of loading large files without blocking the UI thread.

As you may know, Windows Forms imposes the strict rule that a control cannot be accessed from any thread other than the one it was created on. You can Invoke calls to it from another thread, of course, but that method call will still run synchronously on the UI thread (yes, even if you use BeginInvoke). Normally this isn't a problem as the UI isn't doing any heavy processing, because seriously, you don't do heavy processing on the UI thread, right?!

But of course, the RichTextBox is a UI control and loading .rtf files can be very heavy work indeed, but you can't shove it into a background thread and await it, because hey, you can't shove UI controls into a background thread and await them. Argh. You end up with a user experience that feels like 1993 as your form chugs and tries to do its layout while the RichTextBox is hogging up the thread. This is especially egregious if you load a file on startup and display it right away (as AngelLoader does). Then, your form doesn't even get a chance to get into an idle state and at least display a coherent UI while it blocks your input for upwards of an entire second. You could of course do a little await Task.Delay(50); or something to give it some time before doing the file load, but then you're just adding startup time and you also have no way to know just how many milliseconds are enough for the UI render itself.

So this is what I came up with after some research and a lot of trial and error. The eventual goal is to wrap it up nicely into a regular control requiring no special treatment whatsoever. That means it should be fully usable in the designer as well. The user should be able to treat it like a regular RichTextBox and ideally won't even know the difference except that it has extra async methods. I didn't think that could happen at first, but it's looking more promising now. Having said that, AngelLoader is going to use a specially tailored version that integrates better with its custom RichTextBox and that has anything it doesn't need stripped. Cause perf.

But anyway, it's CC0 so you can just grab the code and tailor as you like! Awesome!

About

An experimental method for allowing a WinForms RichTextBox control to be asynchronous for the purpose of loading large files without blocking the UI thread.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published