I have the following code in C#:
namespace Tests
{
[SetUpFixture, RequiresSTA]
public class Setup
{
public IE Window = new IE("webpage");
[SetUp]
public void SetUp()
{
}
[TearDown]
public void TearDown()
{
}
}
}
When I try to run it with my website it returns the error:
"The currentthread needs to have its apartmentstate set to ApartmentState.sta to be able to initiate Internet Explorer"
Normally when using anything except SetupFixture, RequiresSTA it the solution. But for some reason it is not working now.
The solution acctually ended up being rather simple, if you include the line:
[assembly: RequiresSTA]
at the top of your page it will setup the entire assembly to use STA and it no longer throws the error.
You can try starting a new thread and set its ApartmentState:
var t = new Thread(new ThreadStart(ToDo));
t.SetApartmentState(ApartmentState.STA);
t.Start();
// Run synchronously by waiting for t to finish.
t.Join();
And the delegate:
private void ToDo()
{
// Do something...
}
Or inline version:
var t = new Thread(() =>
{
// Do something...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With