描述:
查询符合条件的数据,并通过URL的方式进行提交.
当查询到大量数据时,如何进行多线程提交?
当查询数据的时候(表中数据多点),CPU有时高点能占到30%-50%,我的表都做过索引.是不是可以用多线程查询?从而降低CPU??不知样样理解对不?我现在的源码:
private void time_send()
{
string sql="select * from TB where sendyorn=0"
SqlConnection con = conn();
try
{ SqlDataAdapter adap = new SqlDataAdapter(sql, con); SqlCommandBuilder scb = new SqlCommandBuilder(adap);
DataTable dt = new DataTable();
adap.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
url += "Username=" + this.t_uid.Text.ToString();
string success = succ_flag.Text.ToString();
string DOresult = this.DoUrl(url); //提交URL的方法 dt.Rows[i]["sendyorn"] = 1; //置标识为1,为已检索
adap.Update(dt);
try
{
System.Threading.Thread.Sleep(300); //这样写对不?
}
catch
{
}
Application.DoEvents();
}
}
catch (Exception ep)
{
this.log(ep.Message);
}
finally
{ con.Close();
}
}
查询符合条件的数据,并通过URL的方式进行提交.
当查询到大量数据时,如何进行多线程提交?
当查询数据的时候(表中数据多点),CPU有时高点能占到30%-50%,我的表都做过索引.是不是可以用多线程查询?从而降低CPU??不知样样理解对不?我现在的源码:
private void time_send()
{
string sql="select * from TB where sendyorn=0"
SqlConnection con = conn();
try
{ SqlDataAdapter adap = new SqlDataAdapter(sql, con); SqlCommandBuilder scb = new SqlCommandBuilder(adap);
DataTable dt = new DataTable();
adap.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
url += "Username=" + this.t_uid.Text.ToString();
string success = succ_flag.Text.ToString();
string DOresult = this.DoUrl(url); //提交URL的方法 dt.Rows[i]["sendyorn"] = 1; //置标识为1,为已检索
adap.Update(dt);
try
{
System.Threading.Thread.Sleep(300); //这样写对不?
}
catch
{
}
Application.DoEvents();
}
}
catch (Exception ep)
{
this.log(ep.Message);
}
finally
{ con.Close();
}
}
2、private void time_send()
{
string sql="select * from TB where sendyorn=0"
SqlConnection con = conn();
try
{ SqlDataAdapter adap = new SqlDataAdapter(sql, con); SqlCommandBuilder scb = new SqlCommandBuilder(adap);
DataTable dt = new DataTable();
adap.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
url += "Username=" + this.t_uid.Text.ToString();
string success = succ_flag.Text.ToString();
string DOresult = this.DoUrl(url); //提交URL的方法
dt.Rows[i]["sendyorn"] = 1; //置标识为1,为已检索
adap.Update(dt);
try
{
System.Threading.Thread.Sleep(300); //这样写对不?
}
catch
{
}
Application.DoEvents();
}
}
catch (Exception ep)
{
this.log(ep.Message);
}
finally
{ con.Close();
}
}
红色部分如果占用时间长,可以用异步调用
这个占用时间,因为要链接那个远程URL
如何异步调用
t.start();private void DoSomeThing(){
//add code here
}
//要在线程中执行的方法
})).Start();你上面写的不是多线程。而只是针对当前线程进行的操作。同样还是单线程。
另外你的方法可能有参数。
那么可以用这样的方法
new Thread(new ParameterizedThreadStart(obj)).Start();
但是只能一个参数。如果有方法有多个参数,要自己封装下方法,将多个参数放到一个对象传递过去。
那我是把提交和更新全放在线程里吗?会不会重复提交?
private void Form1_Load(object sender, EventArgs e)
{
new Thread(new ThreadStart(delegate() {
show("neirong", "biaoti");
})).Start();
}
public void show(string text, string title)
{
MessageBox.Show(text, title);
}
如果你的方法中设计到调用其他控件,那就有点麻烦了。需要你写委托了。不然是不能在多线程中调用其他控件的。
我把这个方法放到线程 里,是不是提交速度可以大大提升?
{
string sql="select * from TB where sendyorn=0"
SqlConnection con = conn();
try
{ SqlDataAdapter adap = new SqlDataAdapter(sql, con); SqlCommandBuilder scb = new SqlCommandBuilder(adap);
DataTable dt = new DataTable();
adap.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
url += "Username=" + this.t_uid.Text.ToString();
string success = succ_flag.Text.ToString();
string DOresult = this.DoUrl(url); //提交URL的方法,这个方法里面有没有涉及到其他控件? dt.Rows[i]["sendyorn"] = 1; //置标识为1,为已检索
adap.Update(dt);
/*try
{
System.Threading.Thread.Sleep(300); //这样写对不?
}
catch
{
}
Application.DoEvents();*/不需要这一段
}
}
catch (Exception ep)
{
this.log(ep.Message);//这里使用了this代表啥,是这个窗体?
}
finally
{ con.Close();
}
}你的代码我引用了。里面有我的一些问题。你看看。回答了才能改
catch (WebException Ex)
{
this.log(Ex.Message);
}
这个 this.log(ep.Message);//这里使用了this代表啥,是这个窗体?
是个
private void log(string msg)
{ string time = DateTime.Now.ToString("yyyyMMdd HH:mm:ss:ffff");
this.tb_log.Text = "【" + time + "】" + msg + "\r\n" + this.tb_log.Text;
}
deletage void delevoid0(string s);
private void log(string msg)
{
Control ctl=(Control)tb_log;
if(ctl.InvokeRequired)
{
delevoid0 d=new delevoid0(log);
ctl.Invoke(d,new object[]{msg};
return;
}
string time = DateTime.Now.ToString("yyyyMMdd HH:mm:ss:ffff");
tb_log.Text = "【" + time + "】" + msg + "\r\n" + tb_log.Text;//你实际在多线程中只引用了这个tb_log控件。
}
new Thread(new ThreadStart(delegate() {
time_send();
})).Start();
url += "Username=" + this.t_uid.Text.ToString();
string success = succ_flag.Text.ToString();
这个也需要改是吧