关于线程同步?求助~
是这样的,我定义了两个线程执行同一个方法,总报许多奇怪错误,如:内存已损坏、数据更新失败等
经google,原来要用到线程同步什么的
想问怎么加等待?用sleep是不是线程会释放资源了?
怎样确保一个线程A执行完后让线程B执行;线程B执行完,A、B等待一定时间再轮流执行?
先谢谢各位了
定义两个公共线程:
public static Thread thread;//预警通知线程
public static Thread CQthead;//超期通知线程
//然后构造函数实例化线程
thread.Start("YJ");
CQthead.Start("CQ");
private void SendPickNotice(object Type)
{
DataStore dsItem = new DataStore();
dsItem.LibraryList = "E:\\Net架构\\pbl\\baosi.p.md.escm.wm.dock.pbl";
dsItem.DataWindowObject = "dw_wm_dock_inv_wharf_freedate";
dsItem.SetTransaction(trans);
//定义mip到港通知接口参数
string subject = string.Empty;
string type = Type.ToString();
System.Configuration.AppSettingsReader appread = new System.Configuration.AppSettingsReader();
string templateId = string.Empty;//获取模版ID
//templateId = (string)appread.GetValue("DQTemplateId", templateId.GetType()); ;
templateId = "140c404c0887e0e33e9bc1e4675adaee";
StringBuilder values = new StringBuilder();//表单数据,json
string flowstatus = "20";//默认20为流程待审状态
string flowParams = string.Empty;//获取流程参数
string noticeType = "CQTZ";//通知类型为:超期通知代码
string result = string.Empty;//获取web服务接口的返回信息
Dictionary<string, string> Unit = new System.Collections.Generic.Dictionary<string, string>();
while (1 == 1)
{
//dsItem.SetTransaction(trans);
dsItem.Retrieve();
if (type == "YJ")//主题
{
subject = "预警通知";
MessageBox.Show("预警通知启动");
}
else
{
subject = "超期通知";
MessageBox.Show("超期通知启动");
}
//DataWindowControl dsItem = new DataWindowControl();
//分类客户
for (int i = 1; i <= dsItem.RowCount; i++)
{
try
{
Unit.Add(Help.GetItemDecimalNotNull(dsItem, i, "customer_id").ToString(), Help.GetItemStringNotNull(dsItem, i, "customer_name"));
}
catch (ArgumentException)
{
continue;
}
}
for (int i = 0; i < Unit.Count; i++)//根据客户发送通知
{
//dwCustomer = dwMain;
int sub = 0;//用于记录没有超期信息的次数
int customID = Convert.ToInt32(Unit.ElementAt(i).Key);//客户id
string customName = Unit.ElementAt(i).Value;//客户名称
string sqlw = string.Empty;//预警或超期数据筛选条件
flowParams = GetflowParams(noticeType, customID, customName);//获取客户的处理人
if (flowParams == null) return;//检查节点处理人信息
if (type == "YJ") sqlw = string.Format("customer_id={0} and free_date_days={1}", customID, 999999);//筛选预警数据
else sqlw = string.Format("customer_id={0} and free_date_days={1}", customID, 999999);//筛选超期数据
//dwMain.SetRedrawOff();
dsItem.SetFilter(sqlw);
dsItem.Filter();
//dwCustomer.SetFilter(sqlw);
//dwCustomer.Filter();
if (dsItem.RowCount < 1)
{
sub++;
if (sub == Unit.Count - 1) return;
continue;//如果当前客户没有超期数据,则找下一个客户
}
values = CreateCharacter(dsItem, columnNames, flowParams);
try
{
result = MipInterface.addReviewValue(subject, templateId, values, flowstatus);
if (string.IsNullOrEmpty(result))
{
MessageBox.Show(customName + " 的预警通知发送失败!\n");
return;
}
else
{
if (type == "YJ")
{
for (int j = 1; j <= dsItem.RowCount; j++)
{
dsItem.SetItemDecimal(j, "POST_WARNINGFLAG", 1);//更新预警通知标记
}
try
{
//dsItem.AcceptText();
dsItem.UpdateData();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
MessageBox.Show("通知标记数据更新出错!\n" + ex.ToString());
}
}
else
{
for (int j = 1; j <= dsItem.RowCount; j++)
{
dsItem.SetItemDecimal(j, "POST_ARROGANTFLAG", 0);//更新超期通知标记
}
try
{
//dsItem.AcceptText();
dsItem.UpdateData(true, true);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
MessageBox.Show("通知标记数据更行出错!\n" + ex.ToString());
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(customName + " 的预警通知发送失败!\n" + ex.ToString());
return;
}
}
//dsItem.Reset();
System.Threading.Thread.Sleep(10000);//睡眠10秒
}
}