解决问题马上给分
我用timer_tick时间 每30分钟 去执行,取设备里的信息,昨天晚上9点开启后,今天早上来程序还在运行,可是取一个设备信息
由刚开始的5秒变成了2分钟。
是何原因?
public class 线程操作类{ public object 设备信息; public void 读取数据方法() { //读取并写入数据库 }}timer_Tick(object sender, EventArgs e){ BackgroundWorker[] bgws = new BackgroundWorker[设备总数]; 线程操作类[] temps = new 线程操作类[设备总数]; for(int i=0;i<设备总数,i++){ //实例化线程和线程操作类 bgws[i] = new BackgroundWorker(); temps[i] = new 线程操作类(); //设置线程工作的方法 bgws[i].DoWork += new DoWorkEventHandler(temps[i].读取数据方法); //启用线程 bgws[i].RunWorkerAsync()}}
System.Timers.Timer timer = new System.Timers.Timer(5000); for (int i = 0; i < 设备总数; i++) { int j = i;//必须再给一个 变量,否则后面的委托将用i的最后一个值 timer.Elapsed += delegate { //这里写对应设备的处理方法 temps[j].读取数据方法 }; } timer.Start();