2012-10-05 23 views
6

Ho il seguente codice in C#:Come posso correggere l'errore: "Il currentthread deve avere il suo stato di appartamento impostato su ApartmentState.sta per poter avviare Internet Explorer"?

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

     [SetUp] 
     public void SetUp() 
     { 

     } 

     [TearDown] 
     public void TearDown() 
     { 

     } 
    } 
} 

Quando provo a farlo funzionare con il mio sito web restituisce l'errore:

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

Normalmente quando si utilizza nulla, tranne SetupFixture, RequiresSTA la soluzione. Ma per qualche motivo non funziona ora.

risposta

10

La soluzione acctually finito essere piuttosto semplice, se includi la riga:

[assembly: RequiresSTA] 

nella parte superiore della pagina verrà configurato l'intero assembly per utilizzare STA e non genera più l'errore.

5

Si può provare ad avviare un nuovo thread e impostare la sua ApartmentState:

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

E il delegato:

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

o una versione in linea:

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