日期:2014-05-18 浏览次数:20853 次
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections; using System.Collections.Specialized; using System.Collections.Generic; using System.Threading; using System.Text; namespace MyProgram { public class OrderBaseObj : MarshalByRefObject { // 待处理订单列表 protected static List<OrderItem> lstUnHandle = new List<OrderItem>(); // 已处理订单列表 protected static List<OrderItem> lstHandle = new List<OrderItem>(); // 初始化是否完成标记 protected static bool blnInitFinished = false; // 待执行的数据库事务表 protected static List<string> lstUpdateCommand = new List<string>(); // 初始化线程 protected static Thread threadInit = null; // 处理线程 protected static Thread threadHandle = null; // 数据库同步线程 protected static Thread threadDB = null; static OrderBaseObj() { ThreadStart tst = new ThreadStart(ThreadInit_Start); threadInit = new Thread(tst); threadInit.Priority = ThreadPriority.BelowNormal; threadInit.Start(); tst = new ThreadStart(ThreadHandle_Start); threadHandle = new Thread(tst); threadHandle.Priority = ThreadPriority.Lowest; threadHandle.Start(); tst = new ThreadStart(ThreadDB_Start); threadDB = new Thread(tst); threadDB.Priority = ThreadPriority.Lowest; threadDB.Start(); } /// <summary> /// 添加一个PV到待处理区 /// </summary> public void AddPV(OrderItem order) { lstUnHandle.Add(order); } // 初始线程启动方法 private static void ThreadInit_Start() { Init(); blnInitFinished = true; } // 初始化抽象方法 protected static abstract bool Init(); // 处理线程启动方法 private static void ThreadHandle_Start() { while (true) { OrderItem order=lstUnHandle[0]; lstUnHandle.RemoveAt(0); HandleOrder(order); lstHandle.Add(order); if(lstHandle.Count>100) { string strCommmand=HandleUpdateCommmand(); lstUpdateTrans.Add(strCommand); lstHandle.Clear(); } } } // 订单处理的抽象方法,每个子类必须实现该方法 protected abstract static void HandleOrder(OrderItem order); // 返回处理数据库同步更新命令 protected abstract static string HandleUpdateCommmand(); // 同步线程启动方法 private static void ThreadDB_Start() { while (true) { if (lstUpdateTrans.Count > 0) { string strCommand=lstUpdateTrans[0]; lstUpdateTrans.RemoveAt(0); DBObj db=new DBObj(); db.Execute(strCommand); db.CloseConn(); } Thread.Sleep(new TimeSpan(0, 0, 2)); } } } }