在做一个基于Sql Server 2005的BS项目,项目大致要求是这样的,根据数据库中某条信息的处理时间,然后该条信息时间到了,我要将相应的处理结果添加到表中,之前试开发过Windows服务每秒不断的扫描数据库,当发现需要处理的信息,然后添加处理结果,但是这种系统开销太大,目前我考虑的思路是这样的,在信息添加的时候调用 Clr存储过程,通知处理的结束时间,然后我定义线程池,添加定时器,到那个时间之后连接数据库,添加处理的结果到表中,测试发现很占用Cpu哟,只要稍微多点信息,电脑就死机一样,不知道什么原因,请高手赐教,或者有更好的解决办法,请说一下,只要解决问题,分值不是问题!public partial class AutoResult
{
#region 调用存储过程处理结果
public static void AutoAddResult(int ProductID)
{
//这里调用我数据库中的存储过程进行结果处理
}
#endregion #region 构造函数
static AutoResult()
{
//构造函数
DbHelperSQL.OpenConnect();//该句代码是打开数据库连接 }
#endregion #region 线程时间到了的处理办法
public static void SQLThread(object ID, bool bTimeout)
{
if (DbHelperSQL.connection == null)
{
DbHelperSQL.OpenConnect();
}
//准备执行存储过程
int ProductID = Convert.ToInt32(ID);
AutoResult.AutoAddResult(ProductID);
}
#endregion [Microsoft.SqlServer.Server.SqlProcedure]
public static void SpAutoResult(int ProductID,DateTime EndDateTime)
{
AutoResetEvent wh = new AutoResetEvent(false);
int DistinceTimeS = Convert.ToInt32(GetDateDiff.DateDiff(GetDateDiff.DateInterval.Milliseconds, DateTime.Now, EndDateTime));//这里得到了处理时间和当前时间相差的毫秒数
ThreadPool.RegisterWaitForSingleObject(wh, new WaitOrTimerCallback(SQLThread), ProductID, DistinceTimeS, true);
}
};
{
#region 调用存储过程处理结果
public static void AutoAddResult(int ProductID)
{
//这里调用我数据库中的存储过程进行结果处理
}
#endregion #region 构造函数
static AutoResult()
{
//构造函数
DbHelperSQL.OpenConnect();//该句代码是打开数据库连接 }
#endregion #region 线程时间到了的处理办法
public static void SQLThread(object ID, bool bTimeout)
{
if (DbHelperSQL.connection == null)
{
DbHelperSQL.OpenConnect();
}
//准备执行存储过程
int ProductID = Convert.ToInt32(ID);
AutoResult.AutoAddResult(ProductID);
}
#endregion [Microsoft.SqlServer.Server.SqlProcedure]
public static void SpAutoResult(int ProductID,DateTime EndDateTime)
{
AutoResetEvent wh = new AutoResetEvent(false);
int DistinceTimeS = Convert.ToInt32(GetDateDiff.DateDiff(GetDateDiff.DateInterval.Milliseconds, DateTime.Now, EndDateTime));//这里得到了处理时间和当前时间相差的毫秒数
ThreadPool.RegisterWaitForSingleObject(wh, new WaitOrTimerCallback(SQLThread), ProductID, DistinceTimeS, true);
}
};
/// 启动监听,轮询监听客户机请求并将客户端套接字存入转发表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Listen()
{
IPAddress _ip = IPAddress.Any;
Socket newsoc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
newsoc.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
IPEndPoint locaEp = new IPEndPoint(IPAddress.Any, _port);//建立连接
try
{
newsoc.Bind(locaEp);
newsoc.Listen(100);
newsoc.BeginAccept(new AsyncCallback(onCall), newsoc);//异步监听回调
}
catch (Exception ex)
{
DAL.Log.Write(DateTime.Now.ToString() + ex.ToString() + "\r\n\r\n");
}
} /// <summary>
/// 监听回调
/// </summary>
/// <param name="ar"></param>
private void onCall(IAsyncResult ar)
{
try
{
Socket serverSoc = (Socket)ar.AsyncState;
Socket clent = serverSoc.EndAccept(ar);
byte[] comes = new byte[1024];
EndPoint enp = clent.RemoteEndPoint;
clent.Send(comes, comes.Length, 0);
_transmit_tb.Add(clent.RemoteEndPoint, null);
serverSoc.BeginAccept(new AsyncCallback(onCall), serverSoc);
while (true)
{
byte[] buffer = new byte[1024];
int re = clent.Receive(comes);
clent.Send(Encoding.ASCII.GetBytes("ok!"));
string msg = Encoding.UTF8.GetString(comes, 0, re);
string ip = clent.RemoteEndPoint.ToString();
if (msg.Length == 0)
{
_transmit_tb.Remove(clent.RemoteEndPoint);
_receviccethread.Interrupt();
_receviccethread.Abort();
break;
} ArrayList MessageRet = BLL.ClientLib.AdapterFactory.Prepare(msg, ip);//转入适配器并返回解析后数据 if (MessageRet != null)
{
BLL.ClientLib.ResloveBuffer resbuf = new BLL.ClientLib.ResloveBuffer();
resbuf.Prepare(MessageRet[0].ToString(), ip);//将数据二次解析后保存数据库
}
}
}
catch (Exception ex)
{
DAL.Log.Write(DateTime.Now.ToString() + ex.ToString() + "\r\n\r\n");
}
}使用 异步的操作可以相当于 半个 线程池了
第二个线程处理 1-20的,每个线程中的循环处理需要sleep 500ms 保证同时运行一定量的线程。
前面线程处理完停掉后新开一个线程处理新的数据,每次开线程直间可能要sleep一定时间。
可以试下Job、触发器或者SqlDependency来触发事件