C#程序操作大量数据Form UI像死机一样,想用委托怎样解决?
private void StartOnce(){
string status_=DataShare(dt.Rows[i][1].ToString(),txt_MaxCount.Text,chk_ifSim.Checked,label18)
}
private string DataShare(string sTable,string sCount,bool ifSim,System.Windows.Forms.Label lb)
{
//循环执行远程插入数据库 并更新本地数据库标志
int err = 0;
int sec = 0;
for (int i = 0; i < dt.DefaultView.Count; i++)
{
sql = "insert into t_users";
sql +=" select top 1 * from " + sTable + " where "+ dr[0].ToString() +"='"+ dt.Rows[i][0].ToString() +"';";
try
{
SqlData.ExecuteNonQuery(sCon, CommandType.Text, sql);
}
catch
{
err = err + 1;
}
sec = sec + 1;
lb.Text = "表" + sTable + " s:" + sec + ";f:" + err + "";
lb.Refresh();
}
return err+","+sec;
} --------------------编程问答-------------------- 在执行操作数据时还要通知界面的label执行到哪里,很苦恼啊,希望大家帮帮忙,谢谢 --------------------编程问答-------------------- 路过,帮顶一下 --------------------编程问答-------------------- LZ试试在子线程做大量的、耗时的工作,必要时通知主线程更新状态就可以了:
--------------------编程问答-------------------- 出错了,txt_MaxCount.Text,chk_ifSim.Checked,label18都未引用,开了线程就脱离了ui的控件么? --------------------编程问答-------------------- 错误 5 参数“1”: 无法从“void”转换为“System.Threading.ThreadStart"
private void StartOnce()
{
Thread soi = new Thread(StartOnceImp);
soi.Start();
}
private void StartOnceImp()
{
string status_=DataShare(dt.Rows[i][1].ToString(),txt_MaxCount.Text,chk_ifSim.Checked,label18)
}
private string DataShare(string sTable,string sCount,bool ifSim,System.Windows.Forms.Label lb)
{
//循环执行远程插入数据库 并更新本地数据库标志
int err = 0;
int sec = 0;
for (int i = 0; i < dt.DefaultView.Count; i++)
{
sql = "insert into t_users";
sql +=" select top 1 * from " + sTable + " where "+ dr[0].ToString() +"= '"+ dt.Rows[i][0].ToString() +" ';";
try
{
SqlData.ExecuteNonQuery(sCon, CommandType.Text, sql);
}
catch
{
err = err + 1;
}
sec = sec + 1;
// 修改了这两句通知主线程
// lb.Text = "表" + sTable + " s:" + sec + ";f:" + err + "";
// lb.Refresh();
NotifyUI(lb, "表" + sTable + " s:" + sec + ";f:" + err + "");
}
return err+","+sec;
}
delegate void NotifyUICallback(object sender, string text);
private void NotifyUI(object sender, string message)
{
Label lb = sender as Label;
if(lb != null)
{
if (lb.InvokeRequired)
{
NotifyUICallback anc = new NotifyUICallback(NotifyUI);
lb.Invoke(anc, new object[] {sender, message});
}
else
{
lb.Text = message;
lb.Refresh();
}
}
}
不好意思,线程这一直是没搞明白.需要用static么? --------------------编程问答-------------------- 我用的是2005 --------------------编程问答-------------------- 成功了,谢谢lalac --------------------编程问答-------------------- 采用子线程进行数据库操作不就行了。 --------------------编程问答-------------------- 我发现主要是在关闭串口时会出现这种死机的情况。但又并不是每回必死,而是有时死,有时又不死。
补充:.NET技术 , C#