简体   繁体   中英

.Net Core web application running STA thread

I would like to run GET method in controller in STA mode. For Web API this worked perfectly: http://ryanhaugh.com/archive/2014/05/24/supporting-sta-threads-in-web-api/ Can it be translated to .NET Core?

I do it the same way now, except I create my thread differently.

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

The IsBackground and Priority part is optional of course.

Hope this helps someone

To use STA I created new thread:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM