STAThrea

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I just tried to compile an 'old' .net framework 1.1 application with vs2005
- and suddenly the code throws some error in runtime:

ThreadStateException:
Current thread must set to single thread apartment (STA) mode before OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it.

I tried to set the apartment state with SetApartmentState() but this failed
also ('couldn't set apartment state').

Can anyone help me out of this?

Thanks
Peter
 
Peter Schmitz said:
I just tried to compile an 'old' .net framework 1.1 application with vs2005
- and suddenly the code throws some error in runtime:

ThreadStateException:
Current thread must set to single thread apartment (STA) mode before OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it.

I tried to set the apartment state with SetApartmentState() but this failed
also ('couldn't set apartment state').

Can anyone help me out of this?

Usually it's easiest just to put the STAThreadAttribute on Main, but it
depends on exactly what you're doing.

What thread is the failing call running on?
 
Peter Schmitz said:
Hi,

I just tried to compile an 'old' .net framework 1.1 application with
vs2005
- and suddenly the code throws some error in runtime:

ThreadStateException:
Current thread must set to single thread apartment (STA) mode before OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it.

I tried to set the apartment state with SetApartmentState() but this
failed
also ('couldn't set apartment state').

Can anyone help me out of this?

Thanks
Peter

That's the result of a new probe in v2 that checks the thead's apartment
when calling into COM.
What error did you get when calling SetApartmentState() and on what thread
did you called this.

If it's the main thread do as Jon said, if its on a background thread, you
better call SetApartmentState before you start the thread, don't do it in
the thread procedure.

Willy.
 
Back
Top