/// <summary> /// 能否开始一个互斥操作,用完后请CloseMutex() /// </summary> /// <param name="om"></param> /// <param name="hasErrorPrompt"></param> /// <returns></returns> public bool StartMutex(ObjectMutex om, bool hasErrorPrompt) { MutexStatus stat = GetMutexState(om, true); if (stat == MutexStatus.进行中) { if (hasErrorPrompt) { System.Windows.Forms.MessageBox.Show("用户[" + om.UserName + "]正在 " + om.Digest + ", 预计至多剩余" + om.Timeout + "秒. \n\n" + "本次操作被终止, 请稍后再继续执行.", "检查并发操作", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); } return(false); } return(true); }
/// <summary>检查互斥. 在allowBeginMutex=true时,用完后请CloseMutex() /// ,互斥表ObjectMutex字段:ID=LC.Bal.Sum.Year.Term;BeginTime:起始点(服务器时间);EndTime:完成点(服务器时间);TimeOut:超时秒;UseName:用户名, /// 判断是否互斥 只需要检查(serverdatetime-begintime)是否>timeout即可, /// EndTime可以不填 /// </summary> /// <param name="ID"></param> /// <param name="timeOutSec"></param> /// <param name="UserName"></param> /// <param name="Digest"></param> /// <param name="allowBeginMutex"></param> /// <returns></returns> public MutexStatus GetMutexState(ObjectMutex om, bool allowBeginMutex) { int i = 0; MutexStatus b; #region 建立互斥表 if (!ExistTable("ObjectMutex")) { i = 0; DataColumnEx[] cols = new DataColumnEx[6]; cols[i] = new DataColumnEx(); cols[i].ColumnName = "ID";//对象类型 cols[i].DataType = DbType.String; i++; cols[i] = new DataColumnEx(); cols[i].ColumnName = "BeginTime";//对象枚举 cols[i].DataType = DbType.DateTime; i++; cols[i] = new DataColumnEx(); cols[i].ColumnName = "EndTime";//系统内置 cols[i].DataType = DbType.DateTime; cols[i].AllowDBNull = true; i++; cols[i] = new DataColumnEx(); cols[i].ColumnName = "TimeOut";//对象类型 cols[i].DataType = DbType.Int32; i++; cols[i] = new DataColumnEx(); cols[i].ColumnName = "UserName";//对象枚举 cols[i].DataType = DbType.String; i++; cols[i] = new DataColumnEx(); cols[i].ColumnName = "Digest";//系统内置 cols[i].DataType = DbType.String; CreateTable("ObjectMutex", cols, new string[1] { "ID" }); } #endregion StringBuilder sb = new StringBuilder(); sb.Append("select datediff(s,BeginTime,T2.d) as dd,TimeOut,UserName,Digest from ("); sb.Append(" SELECT getdate() as d,BeginTime,TimeOut,UserName,Digest from ObjectMutex"); sb.Append(" Where ID='" + om.ID); sb.Append("') T2"); string sql = sb.ToString(); SqlCommand cm = new SqlCommand(sql, _dbConn); SqlDataReader rs = cm.ExecuteReader(); if (!rs.HasRows) { b = MutexStatus.停止; } else { rs.Read(); int d = int.Parse(rs.GetValue(0).ToString()); int to = int.Parse(rs.GetValue(1).ToString()); if (d > to) { b = MutexStatus.停止; } else { om.UserName = rs.GetString(2).Trim(); om.Digest = rs.GetString(3).Trim(); om.Timeout = to - d; b = MutexStatus.进行中; } } rs.Close(); if (b == MutexStatus.停止 && allowBeginMutex) { cm = new SqlCommand("delete from ObjectMutex Where ID='" + om.ID + "'", _dbConn); cm.ExecuteNonQuery(); sb = new StringBuilder(); sb.Append("INSERT INTO ObjectMutex ([ID], [BeginTime], [TimeOut], [UserName], [Digest])"); sb.Append(" VALUES('"); sb.Append(om.ID); sb.Append("', getdate() ,"); sb.Append(om.Timeout); sb.Append(",'" + om.UserName); sb.Append("','" + om.Digest + "')"); sql = sb.ToString(); cm = new SqlCommand(sql, _dbConn); i = cm.ExecuteNonQuery(); if (i > 0) { b = MutexStatus.开始; } } return(b); }