日期:2014-05-17 浏览次数:20516 次
using System; using System.Collections.Generic; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Diagnostics; using System.Threading; using System.Runtime.InteropServices; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 锁啊锁 /// </summary> private ReaderWriterLockSlim _RWLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion); // 打卡队列 Queue<SigninInfo> _SigninQueue = new Queue<SigninInfo>(600); /// <summary> /// 打卡签到进入队列 /// </summary> /// <param name="obj"></param> private void Singin(Object obj) { while (true) { _RWLock.EnterWriteLock(); try { SigninInfo entity = new SigninInfo(); entity.EmployeeID = System.Environment.TickCount; entity.SigninTime = System.DateTime.Now; _SigninQueue.Enqueue(entity); } finally { _RWLock.ExitWriteLock(); System.Threading.Thread.Sleep(10); } } } /// <summary> /// 保存 /// </summary> /// <param name="obj"></param> private void Save(Object obj) { while (true) { _RWLock.EnterReadLock(); try { if (_SigninQueue.Count == 0) { continue; } SigninInfo entity = _SigninQueue.Dequeue(); System.Diagnostics.Debug.WriteLine(String.Format("员工ID={0},打卡时间={1}", entity.EmployeeID, entity.SigninTime)); } finally { _RWLock.ExitReadLock(); System.Threading.Thread.Sleep(10); } } } private void button1_Click(object sender, EventArgs e) { ThreadPool.QueueUserWorkItem(new WaitCallback(Singin), null); ThreadPool.QueueUserWorkItem(new WaitCallback(Save), null); } } /// <summary> /// 打卡信息 /// </summary> public struct SigninInfo { public Int32 EmployeeID; public DateTime SigninTime; } }