0
Reply

WebBrowser object in thread....

Rahul Shinde

Rahul Shinde

Dec 9 2014 2:35 AM
815
I have an console application which uses webbrowser object which navigate and url which has multiple buttons.. The button tooks 3-4min to complete the operation assigned to button. I want if the button operation took more than 4 min then it terminate the process and move to next button.
for this i wrote  the following sample code but its not working .. Giving an error "'webBrowser1.Document' threw an exception of type 'System.InvalidCastException'"
  public static void googleCall()
{
WebBrowser webBrowser1 = new WebBrowser();
webBrowser1.Navigate("https://www.google.co.in");
webBrowser1.ScriptErrorsSuppressed = true;
bool isReady = false;
Application.DoEvents();
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
isReady = false;
}
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
isReady = true;
}
if (isReady)
{
List<string> x = new List<string>();
  x.Add("1");
                x.Add("2");
                x.Add("3");
foreach (var x1 in x)
{
int qryTimeOut = 200000;
ManualResetEvent wait = new ManualResetEvent(false);
Thread work = new Thread(new ThreadStart(() =>
{
GetResponse(webBrowser1);
wait.Set();
}));
work.SetApartmentState(ApartmentState.STA);
work.Start();
Boolean signal = wait.WaitOne(qryTimeOut);
if (!signal)
{
work.Abort();
}
wait.Dispose();
}
}
}
public static void GetResponse(WebBrowser Browser)
{

}

Error as follows: