由于要进行数据处理就写了一个windows服务程序,但是有时会自动死掉的,我再重新启动一个服务就可以正常运行了。不知道为什么?
public class test{private static object obj2 = new object();
static bool flage2= true;public void UpdateTrigger()
{
string s_Sql = "select * from PB_Code_Trigger ";
lock (obj2)
{
if (flage2== true)
{
flage2 = false;
StringBuilder sb = new StringBuilder("");
DataSet ds = SqlHelp.QueryDataSet(s_Sql, this.SqlConnectString);
foreach (DataRow row in ds.Tables[0].Rows)
{
if (row["PCE_CementDetils"].ToString() == "1" && row["PCE_DaySell"].ToString() == "1" && row["PCE_Receive"].ToString() == "1" && row["PCE_XsDetial"].ToString() == "1")//&& row["PCE_Rotarykiln"].ToString() == "1"&& row["PCE_RunDetail"].ToString() == "1"
{
sb.Append(" update PB_Code_Trigger set PCE_CementDetils='0',PCE_DaySell='0',PCE_Receive='0',PCE_XsDetial='0',PCE_Date='" + DateTime.Now.ToShortDateString() + "' where PCE_Filiale='" + row["PCE_Filiale"].ToString() + "';");
sb.Append(" delete " + row["PCE_DbName"].ToString() + ".dbo.x_selltrigger where stime<='" + DateTime.Now.AddDays(-1).ToShortDateString() + "';");
}
}
if (sb.Length > 0)
{
SqlHelp.Update(sb.ToString(), this.SqlConnectString, null);
}
flage2 = true;
}
}
}//使用的是多线程操作
请会我这里用加锁吗?服务每一分种执行一次。如果我还有一个方法里面也用到了下列代码
lock (obj2)
{
if (flage2== true)
{
//dosome
flage2 = false;
}
}
这样obj2和flage2和第一个方法有冲突吗?