0

I am using an undocumented library which came as an OSX file. I used AxImp.exe to create a dll file (Named AxLibraryName) and used it for my code.

When I initiate the class like this:

AxLibraryName n = new AxLibraryName();

I get a ThreadStateException. When I do it in a different thread like this:

var t = new Thread(SubClassThatInitiateAxLibraryName.Initiate);
t.SetApartmentState(ApartmentState.STA);
t.Start();

It works, but I cannot use any of the SubClassThatInitiateAxLibraryName methods since SubClassThatInitiateAxLibraryName.AxLibraryName is Null afterwards. (Of course, I call it inside some kind of SubClassThatInitiateAxLibraryName method, and it is called this.AxLibraryNameBlah)

Is there a way to initiate this library in main.cs?

Thanks :)

Yotam Vaknin
  • 676
  • 4
  • 19
  • This is normal, an ActiveX control must be created on an STA thread. Such a thread must stay alive and pump a message loop while the component is used. Methods on that control must normally be made on the same thread as well. A Winforms project is the appropriate kind of project for such a component, not a console project. Check [this answer](http://stackoverflow.com/a/21684059/17034) for a class that creates a hospitable home for such a control. – Hans Passant Mar 19 '14 at 21:24
  • Thank you very much! I am a bit new to this, would you be able to explain a bit more on how should I use the class in my code? – Yotam Vaknin Mar 19 '14 at 22:21
  • Hmya, there are plenty of sharp edges here, threading is never a small detail and a "bit more" won't help you. Use a Windows Forms project and you won't have any problems with that component. – Hans Passant Mar 19 '14 at 22:28

0 Answers0