日期:2014-05-18 浏览次数:20886 次
public class Printer{ public string Name { get; set; } //打印机名称 public int Connecttime { get; set; } //超时时间 public string IP { get; set; } //打印机IP public string Status{get;set;} //打印机状态 需要更新回主界面 public bool Open(){} //打开打印机 public bool Close(){} //关闭打印机 public bool Write(string strdata){} //打印机写内容 }
List<Printer> printers = new List<Printer>(); private void Form1_Load(object sender, EventArgs e) { // 另外有一张表维护了所有打印机的信息 printers.Add(xxxx); // // 然后针对每个打印机开个线程 foreach (Printer p in printers) //循环所有打印机 { //这里创建线程 } }
class Program { /// <summary> /// 打印任务管理类 /// </summary> public class JobManager { public delegate void JobHasGotDel(object sender, JobManagerEventArgs e);//取到打印任务委托 public event JobHasGotDel JobHasGot;//取到打印任务事件 private int _printtimes = 5;//打印机数量 public void StartGetJob(int printtimes) { this._printtimes = printtimes; Thread getjobthread = new Thread(new ParameterizedThreadStart(GetJobProc)); getjobthread.Start(); } private void GetJobProc(object obj)//取打印任务线程 { while (true) { Random ran = new Random(); int rantime = ran.Next(5000); Thread.Sleep(rantime);//模拟取打印任务时间消耗 jobqueue.Enqueue(new PrintJob("job" + rantime.ToString(), _printtimes));//取到后加入打印任务队列 Console.WriteLine("job " + rantime.ToString() + " got!"); if (JobHasGot != null) JobHasGot(this, new JobManagerEventArgs(_printtimes));//引发已取到打印任务事件 } } } /// <summary> /// 打印任务管理参数类 /// </summary> public class JobManagerEventArgs : EventArgs { private int _printtimes;//打印次数/打