C#线程参数时报:必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。如何解决?

问题
在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。

解决
Thread thread = new Thread(new ParameterizedThreadStart(threadInvoke));
// 设置单线程单元模式
thread.SetApartmentState(ApartmentState.STA);
thread.Start(sb);