Task加载数据

//父级

string F_Sql = "select id,code,name from ftable where deleted=0 order by convert(int,code)";

DataTable F_dt = SqlHlper.ExecuteDt(F_Sql);
string J_AcInfo = "[";

//父级数据条数
int count = F_dt.Rows.Count;

//分进程 5条一进程
int rowcount = 0;
if ((count % 5) > 0)
{
rowcount = (count / 5) + 1;
}
Task[] task = new Task[rowcount];
//存放检测的结果
Dictionary<string, string> GetIPZT = new Dictionary<string, string>();
for (int k = 0; k < rowcount; k++)
{
List<DataRow> dt_row = F_dt.AsEnumerable().ToList().Skip(k * 5).Take(5).ToList();
task[k] = Task.Factory.StartNew(()=>{
foreach (var item in dt_row)
{
string AcInfo = "{";
AcInfo += "\"f_no\":\"" + item["code"].ToString() + "\",";
AcInfo += "\"f_name\":\"" + item["name"].ToString() + "\",";
AcInfo += "\"ac_data\":[ ";

//查询子数据

string Ac_Sql = "select * from c";

DataTable Ac_dt = SqlHlper.ExecuteDt(Ac_Sql);
for (int j = 0; j < Ac_dt.Rows.Count; j++)
{
//获取状态
string zt = "失败";
string key = Ac_dt.Rows[j]["a"].ToString() + "-" + Ac_dt.Rows[j]["b"].ToString();
//判断是否有检测过,有则不继续检测直接获取值,无则需检测
if (!GetIPZT.ContainsKey(key))
{
//根据键查询对应的值
zt = GetIPZT[key];
}

AcInfo += "{\"sn\":\"" + Ac_dt.Rows[j]["sn"].ToString() + "\",\"ip\":\"" + Ac_dt.Rows[j]["ip"].ToString() + "\",\"wh\":\"" + Ac_dt.Rows[j]["wh"].ToString() + "\"},";
}
J_AcInfo += AcInfo.Substring(0, AcInfo.Length - 1) + "]},";
}

});
}

//等待所有进程执行完成
Task.WaitAll(task);
J_AcInfo = J_AcInfo.Substring(0, J_AcInfo.Length - 1) + "]";
return J_AcInfo;

posted @ 2019-10-21 09:34  天空之云  阅读(116)  评论(0编辑  收藏  举报