Reputation: 9090
In Watin's source code, there is this piece of code:
public void NavigateToNoWait(Uri url)
{
var thread = new Thread(GoToNoWaitInternal);
thread.SetApartmentState(ApartmentState.STA);
thread.Start(url);
thread.Join(500);
}
[STAThread]
private void GoToNoWaitInternal(object uriIn)
{
var uri = (Uri)uriIn;
NavigateTo(uri);
}
Since the thread created has its apartment state set, why is the [STAThread]
attribute added to the method? I am not interested in the specific piece of code, but I am wondering if STAThread
attribute is needed at all.
Notes:
GoToNoWaitInternal
isn't used elsewhere.Upvotes: 8
Views: 10859
Reputation: 51
It should be noted that the STA (Single Threaded Apartment) is the threading model used by pre-.Net Visual Basic. It should only be used on the Main method of components that will be exposed to COM. The author of the code that you are trying to understand, appearantly did not understand how it is supposed to be used.
Upvotes: 3
Reputation: 244968
Just read the documentation for STAThreadAttribute
(emphasis mine):
Apply this attribute to the entry point method (the
Main()
method in C# and Visual Basic). It has no effect on other methods. To set the apartment state of threads you start in your code, use theThread.SetApartmentState
method before starting the thread.
So, in this case, the attribute should have no effect.
Upvotes: 8