Reputation: 7979
I am calling a COM method from a class A
So to make thread Model of .Net same to Single Threaded Apartment
I am calling following line of code in class A’s constructor
Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
But I am getting
Invalid operation exception “Failed to set the specified COM apartment state”
What can I do to set current thread to Single Threaded Apartment
Upvotes: 0
Views: 2213
Reputation: 8584
Call the COM method on a new thread and set that thread's state instead:
Thread newThread =
new Thread(new ThreadStart(MethodToCallCOMMethod));
newThread.SetApartmentState(ApartmentState.STA);
newThread.Start();
Upvotes: 4