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();
/* ... */
}
但是,当我使用上面的代码时,它占用了我33%的处理器使用率,我正在尝试打开2个程序,它打了100%我的CPU,请问如何减少烧毁我的处理器之前的CPU使用率?我在代码上做错了吗?