在Winform中线程更新UI线程
例如:Form中有一个DataGridView,我们使用Thread查询后,更新这个表格,如果在Thread中直接更新会报错。
Thread th = new Thread((object para) =>
{
List<GetBatAddNumberRetModels> ret = Api.Send("xxx", "xxx", para.GetString(), sendType.json).UNZIP().formatJson<List<GetBatAddNumberRetModels>>();
DataTable dt = smsTable.Clone();
foreach (var m in ret)
{
var temp = common.smsTempList.Where(k => k.id == m.smsId).FirstOrDefault();
DataRow row = dt.NewRow();
row["sms_check"] = false;
row["sms_number"] = m.nmb.GetString();
row["sms_batid"] = m.id.Int64();
row["sms_submitdate"] = m.date.ToString("yyyy-MM-dd HH:mm:ss");
row["sms_tempname"] = temp != null ? temp.name : "";
row["sms_auth"] = Enum.GetName(typeof(smsAuthedEnum), m.smsAuthed);
row["sms_expdate"] = m.smsExpDate == DateTime.MinValue ? "" : m.smsExpDate.ToString("yyyy-MM-dd HH:mm:ss");
row["sms_senddate"] = m.smsSendDate > DateTime.MinValue ? m.smsSendDate.ToString("yyyy-MM-dd HH:mm:ss") : "";
row["sms_sendstatus"] = Enum.GetName(typeof(smsSendStatusEnum), m.smsSendStauts);
row["sms_tempid"] = m.smsId;
row["sms_result"] = m.smsAuthInfo.GetString().Trim();
row["sms_smstype"] = m.smsExtType.GetString().Trim();
row["sms_smsleave"] = m.smsExtLeave.GetString().Trim();
row["sms_smsactive"] = m.smsExtActive.GetString().Trim();
dt.Rows.Add(row);
}
if (this.IsHandleCreated)
{
this.BeginInvoke(new EventHandler(delegate(Object sd, EventArgs ag)
{
DataTable tab = (DataTable)sd;
smsView.DataSource = tab;
}), new object[] { dt });
}
});
th.IsBackground = true;
th.Start(JsonConvert.SerializeObject(query));