System.Threading.Timer 使用问题!!
System.Threading.Timer[] threadTimers = null;
threadTimers = new System.Threading.Timer[dsClientList.Tables[0].Rows.Count];
//GC.KeepAlive(threadTimers);
foreach (DataRow drow in dsClientList.Tables[0].Rows)
{
KkInfor infor = new KkInfor();
infor.kkip = drow["IP"].ToString();
infor.kkconstr = drow["ConnectionString"].ToString();
infor.kkname = drow["Name"].ToString();
threadTimers[count] = new System.Threading.Timer(new TimerCallback(Excetu), infor, 0, 2000);
GC.KeepAlive(threadTimers[count]);
}
public void Excetu(object kk)
{
Console.WriteLine(((KkInfor)kk).kkbh, ((KkInfor)kk).kkconstr, ((KkInfor)kk).kkconstr);
}
--------------------------
我定义一个线程定时器数组,执行一组不同的任务。
但是问题是线程组隔一段时间后就被垃圾回收了。必须GC.KeepAlive来保持线程组不被垃圾回收器回收。
就是这样隔一段时间后就有一个定时器不再执行。