The entry point of a Windows Forms application lacks the single-threaded apartment attribute

tx0/csharp:sta-thread-missingrecommendedwarningreliability/concurrencydepth fastbug

What it finds

Windows Forms needs its UI thread to be a single-threaded COM apartment: clipboard, drag and drop and the common dialogs fail or hang otherwise. The main thread gets that apartment only when Main is marked [STAThread]. Reported is a static Main without the attribute that starts a Windows Forms message loop with Application.Run, in a file that imports System.Windows.Forms.

From the rule's source, sta_thread_missing.rs.

Example from the rule's tests
using System.Windows.Forms; static class Program{    static void Main()tx0 reports: Main starts a Windows Forms message loop without [STAThread]    {        Application.EnableVisualStyles();        Application.Run(new Form());    }} static class Tool{    [System.STAThread]    static void Main(string[] args)    {        Application.Run(new Form());    }     static void Start()    {        Application.Run(new Form());    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:sta-thread-missing" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:sta-thread-missing] reason="..."

tx0 explain tx0/csharp:sta-thread-missing shows the same in the terminal. Found a false positive? Report it.