Kuttan Sujith
Kuttan Sujith

Reputation: 7979

Failed to set the specified COM apartment state ApartmentState.STA

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

Answers (1)

artm
artm

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

Related Questions