运行STA线程的.Net Core Web应用程序

时间:2018-10-26 22:58:30

标签: c# asp.net-core-2.0 sta

我想在STA模式下的控制器中运行GET方法。 对于Web API,这可以完美地工作: http://ryanhaugh.com/archive/2014/05/24/supporting-sta-threads-in-web-api/ 可以将其翻译为.NET Core吗?

2 个答案:

答案 0 :(得分:1)

除了创建线程不同之外,我现在以相同的方式进行操作。

Example example = new Example();
Thread thread =  = new Thread(
        delegate () //Use a delegate here instead of a new ThreadStart
        {
              example.StaRequired(); //Whatever you want to call with STA
        }
)
{
    IsBackground = false,
    Priority = ThreadPriority.Normal
};
thread.SetApartmentState(ApartmentState.STA);
thread.Start(); //Start the thread
thread.Join(); //Block the calling thread

IsBackgroundPriority部分是可选的。

希望这对某人有帮助

答案 1 :(得分:0)

要使用STA,我创建了新线程:

ComClass c = new ComClass();
Thread t = new Thread(new ThreadStart(() => 
{
    c.StaRequired();
}));
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();