June 26, 2025

Introducing Raymond Chen's new book "How to Evade Questions"

Introducing Raymond Chen's new book "How to Evade Questions"

In his blog post If you want to set a thread's apartment model via Thread.CurrentThread.ApartmentState, you need to act quickly, this wonderful, wonderful individual, Raymond Chen, said so much and nothing at the same time. Much of veiled explaining, nothing of solution.

The solution is simple, but none of John Skeet, Stepehen Cleary or this Chen dude mention it because they are up to no good: they saw confusion, not knowledge. I saw knowledge in their stead:

var thread = new Thread(() => Clipboard.SetText("your text"));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();

This is it. 4 lines of code that solve all of the STA thread chicanery and shenanigans caused by Microsoft's knowingly and intentionally disobeying the user's [STAThread] attribute on the Main(). Enjoy!

Were it not static, you could create extension methods on Clipboard class and name them along the lines of SetTextSTA or overload them with another parameter ApartmentState apartmentState = ApartmentState.STA and pass default to all of your existing calls. But MS knows better.

Posted by: LinuxLies at 08:33 PM | No Comments | Add Comment
Post contains 180 words, total size 1 kb.




What colour is a green orange?




16kb generated in CPU 0.0122, elapsed 0.0275 seconds.
35 queries taking 0.0198 seconds, 148 records returned.
Powered by Minx 1.1.6c-pink.