长轮循实现,系统消息提醒,经常数据出错!
我做的一个“系统消息提示”,用的一个长轮循实现。单独的一个SqlConnection,其它的功能又用的另一个SqlConnettion
系统的使用人数大概在60-100左右。
现在的情况的是:有时用着用着,系统会提示出错。多刷新几次,就又正常了。
出错的原因是:提示得很乱,我初步判断是因为轮循的链接,同时触发太多,影响了正常功能应用的链接。
sql操作类用的网上下的DBHelper。自己看了下代码没问题。
当把消息提醒功能关闭,系统不会出错。
请教下这个问题应该怎么处理。
轮循的主要代码
int SendTime = 0; //最多等待时间
string eid = Common.Const.GetF("eid");//要查询的用户
int.TryParse(Common.Const.GetF("time"), out SendTime);
int i = 0;//计算超时时间(秒)
while (true)
{
Thread.Sleep(5000);//停留五千毫秒(5秒),以用这个等待时间达到长轮询
i++;
if (i < SendTime)
{
if (eid != "")
{
lock (this)
{
string strWhere = " receiverId=" + eid + " and isRead=0";
int newmsg = Common.DBHelper.GetRecordCount_msg("OA_SysMsg", strWhere);
if (newmsg != 0)
{
context.Response.Write(newmsg);
break;
}
}
}
}
if (i == SendTime)
{
context.Response.Write("fail:无数据");
break;
}
}
数据查询方法
public static int GetRecordCount_msg(string TableName, string StrWhere)
{
SqlConnection conn_msg;
conn_msg = new SqlConnection();
conn_msg.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strsql = "select Count(*) from " + TableName;
if (StrWhere != "")
{
strsql += " where " + StrWhere;
}
SqlCommand cmd_msg = conn_msg.CreateCommand();
conn_msg.Open();
try
{
cmd_msg.CommandText = strsql;
object obj = cmd_msg.ExecuteScalar();
conn_msg.Close();
if (obj == null)
{
return 0;
}
else
{
return int.Parse(obj.ToString());
}
}
finally {
conn_msg.Close();
}
}
系统的使用人数大概在60-100左右。
现在的情况的是:有时用着用着,系统会提示出错。多刷新几次,就又正常了。
出错的原因是:提示得很乱,我初步判断是因为轮循的链接,同时触发太多,影响了正常功能应用的链接。
sql操作类用的网上下的DBHelper。自己看了下代码没问题。
当把消息提醒功能关闭,系统不会出错。
请教下这个问题应该怎么处理。
轮循的主要代码
int SendTime = 0; //最多等待时间
string eid = Common.Const.GetF("eid");//要查询的用户
int.TryParse(Common.Const.GetF("time"), out SendTime);
int i = 0;//计算超时时间(秒)
while (true)
{
Thread.Sleep(5000);//停留五千毫秒(5秒),以用这个等待时间达到长轮询
i++;
if (i < SendTime)
{
if (eid != "")
{
lock (this)
{
string strWhere = " receiverId=" + eid + " and isRead=0";
int newmsg = Common.DBHelper.GetRecordCount_msg("OA_SysMsg", strWhere);
if (newmsg != 0)
{
context.Response.Write(newmsg);
break;
}
}
}
}
if (i == SendTime)
{
context.Response.Write("fail:无数据");
break;
}
}
数据查询方法
public static int GetRecordCount_msg(string TableName, string StrWhere)
{
SqlConnection conn_msg;
conn_msg = new SqlConnection();
conn_msg.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strsql = "select Count(*) from " + TableName;
if (StrWhere != "")
{
strsql += " where " + StrWhere;
}
SqlCommand cmd_msg = conn_msg.CreateCommand();
conn_msg.Open();
try
{
cmd_msg.CommandText = strsql;
object obj = cmd_msg.ExecuteScalar();
conn_msg.Close();
if (obj == null)
{
return 0;
}
else
{
return int.Parse(obj.ToString());
}
}
finally {
conn_msg.Close();
}
}
...全文
相关推荐
Matlab-Simulink 差速小车循路
双差动单元的四轮差动小车的循路控制。
Python 实现给女朋友的每日微信消息提醒!做一个贴心的程序员!!!
文章首发于个人站点 实现微信消息每日贴心提醒 | 隐舍 微信公众号:【DreamHub】 如何做一个贴心的程序员男朋友?从微信每日提醒开始吧!!! 微信消息内容 包括如下: 日期(阳历+阴历); 每日壹句(内容来自爱词霸); 天气预报(内容来自中国天气网): 天气情况; 温度情况; 穿衣指数; 减肥指数; 空气指数; 紫外线指数; 消息效果 功能模块及其Python实现 依赖包 requests==2.22.0 itchat==1.3.10 sxtwl==1.0.7 日期(阳历+阴历) def getDate(): """ Get date: solar + lunar ca
使用SignalR实现消息提醒
使用SignalR实现消息提醒
QQ MSN 自动循环发送消息机
采用VB.net 2.0开发 可以对qq或者msn中其中一个人自动循环发送一条或者多条内容,可以锁定发送窗口,锁定后只对锁定的窗口发送消息!
WPF实现消息提醒(广告弹窗)
由于项目需要检测子系统运行情况,如有异常及时发现,所以实现了次消息弹窗的功能,这是开发前做了一个Dome,希望对相关需要的同僚提供思路!


