开发者

Is there a generic 'FuncSTA' for invoking delegates by a STA thread?

开发者 https://www.devze.com 2023-01-31 19:10 出处:网络
I have an ASP.NET application and need to use some C开发者_开发技巧OM components inside it. I need a wrapper class over Func or Action which creates a new STA thread and run the delegate with that th

I have an ASP.NET application and need to use some C开发者_开发技巧OM components inside it.

I need a wrapper class over Func or Action which creates a new STA thread and run the delegate with that thread or something like this. Do you know such a class or library out of the box or a sample code ?

CodeUsingComComponent.InvokeSTA()


No such method exists.
You can write one yourself:

public static void InvokeSTA(this ThreadStart method) {
    var thread = new Thread(method);
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
}

If you want to invoke it synchronously, add thread.Join().

0

精彩评论

暂无评论...
验证码 换一张
取 消