i am trying to simulate a web browser in the c# code, since the url have the javascript, i am intend to wait him like 100 miliseconds to grab the full content from it, in the end, we cannot use method like web client to direct download from it, so i am trying to use web browser to take the content, the following is my code:
var t = new Thread(() =>
{
while (true)
{
connection = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]);
connection.Open();
SqlCommand cmd = new SqlCommand("SELECT top 200 stock_code FROM update_stock_code", connection);
DataTable dt = new DataTable();
new SqlDataAdapter(cmd).Fill(dt);
cmd = new SqlCommand("DELETE FROM update_stock_code where stock_code in (select TOP 200 * FROM update_stock_code)", connection);
cmd.ExecuteNonQuery();
for (int a = 0; a < dt.Rows.Count; a++)
{
WebBrowser wb = new WebBrowser();
wb.DocumentCompleted += Wb_DocumentCompleted;
wb.ScriptErrorsSuppressed = true;
wb.Tag = dt.Rows[a][0].ToString();
wb.Navigate("URL");
System.Windows.Forms.Application.Run();
}
connection.Close();
connection.Dispose();
if (dt.Rows.Count == 0)
{
Thread.Sleep(300000);
}
}
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
private static void Wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var wb = (WebBrowser)sender;
string content = wb.Document.Body.InnerHtml;
long milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
long milliseconds2 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
if (milliseconds2 - milliseconds > 100)
{
//some code event
break;
}
System.Windows.Forms.Application.DoEvents();
}
System.Windows.Forms.Application.ExitThread();
wb.Dispose();
/* ... */
}
However, when i am using the above code, it was taking me like 33% usage of my processor and i am trying to open 2 program of this and it hit 100% of my CPU, may i ask how to reduce the CPU usage before it burned my processor? Am i doing anything wrong on the code?