Possible Duplicate:
WebBrowser Control in a new thread
I'm trying to automate clicking certain element on a webpage that then spawn a new IE window on each click.
The code below reads a DataTable with one column containing the element IDs I need to click. It then cycles the table and clicks the required element in the Webbrowser webNav.webBrowser1
.
The code works fine the first time it runs but thereafter there are no more click events. If I step through using breakpoints then I can see the code is running fine without a problem. It appears as if it's scheduling the actions which never get triggered.
This is my first time attempting to use threads so I might be doing something fundamentally wrong.
At the moment I'm a bit stumped as to what the problem is?
[STAThread]
private void ClickOptions()
{
DataTable _dataTableTestClone = new DataTable();
_dataTableTestClone = _dataTableTest;
if (_dataTableTest != null)
{
var thread = new Thread(() =>
{
foreach (DataRow row in _dataTableTestClone.Rows)
{
string getclickID = row["ID"].ToString();
webNav.webBrowser1.Invoke(new Action(() =>
{
HtmlElement lit = webNav.webBrowser1.Document.GetElementById(getclickID);
if (lit != null)
{
lit.InvokeMember("click");
}
}));
Thread.Sleep(2000);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
}
}