I'm using WatiN to login a website. WatiN requires STAThread attribute to be applied on the thread that will be running on. Until now all things were good but now I have a long-task. I'm using WPF and need to keep UI responsive. So I have tried something like this:
_downloadTask = new Task(StartDownload, _cancelationToken.Token);
[STAThread]
public override void StartDownload()
{
if(IsLoginNeeded)
{
bool result = Login();
// check the result and do some other work..
}
}
[STAThread]
public bool Login()
{
if (_browser == null)
_browser = new IE(LoginUrl); // exception
}
When I started my Task, I get ThreadStateException on new IE(LoginUrl) :
The
CurrentThreadneeds to have it'sApartmentStateset toApartmentState.STAto be able to automate Internet Explorer.
As you can see I have tried to apply STAThread on my methods but I couldn't succeeded.Now I'm wondering if it's possible to apply STAThread attribute to a thread that is created on runtime or is there another way to do this?