NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

error not in single-threaded apartment -- webbrowser component -- .Net Module adapter

Solved!
Go to solution

Anyone experience how to over come error message :

An exception occurred inside of the call to .NET member 'FormTestTypePassFail':
System.Threading.ThreadStateException: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2'

cannot be instantiated because the current thread is not in a single-threaded apartment.
at System.Windows.Forms.WebBrowserBase..ctor(String clsidString)
at System.Windows.Forms.WebBrowser..ctor()
at ItsOperatorInterface.FormTestTypePassFail.InitializeComponent() in D:\\Barco\\BackedUp\\chth wrkspc\\chth code dev\\chth C#\\ItsAutomation\\ItsOperatorInterface\\FormTestTypePassFail.Designer.cs:line 37
at ItsOperatorInterface.FormTestTypePassFail..ctor() in D:\\Barco\\BackedUp\\chth wrkspc\\chth code dev\\chth C#\\ItsAutomation\\ItsOperatorInterface\\FormTestTypePassFail.cs:line 33 [Error Code: -17500]

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

I get this error on the moment we call the constructor of a System.Windows.Forms that holds a WebBrowser component.

I can overcome error by using a RichTextBox control instead of WebBrowser component.  

 

I Try to build Pass / Fails test step interface based on .Net module that holds a web browser component. 

This to allow test automation for products under test with WebPage access and web API support.

 

Any input or solution is welcome.

 

Thanks,

Christophe  

 

Test PASS FAIL WebBrowser.png

0 Kudos
Message 1 of 3
(10,478 Views)
Solution
Accepted by topic author chth

You need to create and show your Dialog in an STA (Single-Threaded Apartment) thread. This is a requirement of ActiveX controls. By default, TestStand threads are initialized as MTA (Multi-threaded Apartment). There are two ways you can work around this:

 

1) Use TestStand - If you make a seqence call in a new thread you can specify that that thread be an STA thread in the options for that sequence call. Then put your code to display your dialog in its own sequence that you call with that sequence call, and add a wait step after the sequence call to wait for the new thread.

 

2) Create a new thread in your .NET code that's an STA thread and have the thread that calls into your code just wait for that STA thread to finish displaying the dialog. See Microsoft's MSDN help for how to create threads and wait for them in .NET.

 

Hope this helps,

-Doug

Message 2 of 3
(10,473 Views)

Hello Doug,

 

Perfect input.

I tried today both solutions and I prefer proposal 2.

Both solution works for current problem.

 

Thanks,

Christophe

0 Kudos
Message 3 of 3
(10,462 Views)