2012-10-05 9 views
6

Tengo el siguiente código en C#:¿Cómo puedo solucionar el error: "El hilo actual necesita tener su conjunto de apartamentos en ApartmentState.sta para poder iniciar Internet Explorer"?

namespace Tests 
{  
    [SetUpFixture, RequiresSTA] 
    public class Setup 
    { 
     public IE Window = new IE("webpage"); 

     [SetUp] 
     public void SetUp() 
     { 

     } 

     [TearDown] 
     public void TearDown() 
     { 

     } 
    } 
} 

Cuando trato de ejecutarlo con mi sitio web que devuelve el error:

"The currentthread needs to have its apartmentstate set to ApartmentState.sta to be able to initiate Internet Explorer"

Normalmente cuando se utiliza nada más que SetupFixture, RequiresSTA que sea la solución. Pero por alguna razón, no está funcionando ahora.

Respuesta

10

La solución terminó Acctually hasta siendo bastante simple, si incluye la línea:

[assembly: RequiresSTA] 

en la parte superior de su página configurará todo el conjunto para usar STA y ya no arroja el error.

5

Usted puede intentar iniciar un nuevo hilo y establecer su ApartmentState:

var t = new Thread(new ThreadStart(ToDo)); 
t.SetApartmentState(ApartmentState.STA); 
t.Start(); 
// Run synchronously by waiting for t to finish. 
t.Join(); 

y el delegado:

private void ToDo() 
{ 
    // Do something... 
} 

o la versión en línea:

var t = new Thread(() => 
{ 
    // Do something... 
}); 
Cuestiones relacionadas