日期:2014-05-19  浏览次数:20702 次

更改timer的Interval属性的问题(有代码)
for(int   i=0;i <dt.Rows.Count;i++)
{

DataRow   dr   =   dt.Rows[i];
function   f   =   new   function();

f.CompanyID   =   int.Parse(dr[ "CompanyID "].ToString());
f.strSQL   =   dr[ "JobContent "].ToString();
f.t   =   timers[i];
timers[i]   =   new   System.Timers.Timer();
timers[i].Interval   =   (i+1)*1000;

timers[i].Elapsed   +=   new   System.Timers.ElapsedEventHandler(f.theout);
timers[i].AutoReset   =   true;  
timers[i].Enabled   =   true;
}

public   class   function
{

public   string   strSQL;
public   int   CompanyID;
public   System.Timers.Timer   t;
//public   string  
public   function()
{
//
//   TODO:   在此处添加构造函数逻辑
//
}
public   void   theout(object   source,   System.Timers.ElapsedEventArgs   e)  
{  
Function   f   =   new   Function();
DataTable   dt   =   f.GetDataSet(strSQL).Tables[0];

string   filename= " ";
Excel.ApplicationClass   oExcel;
oExcel   =   new   Excel.ApplicationClass();
oExcel.UserControl   =   false;
Excel.WorkbookClass   wb   =   (Excel.WorkbookClass)   oExcel.Workbooks.Add(System.Reflection.Missing.Value);
for(int   j=0;j <dt.Columns.Count;j++)
{
oExcel.Cells[1,j+1]=dt.Columns[j].ToString();
}
for(int   i   =   0;i   <   dt.Rows.Count;   i++)
{
DataRow   dr   =   dt.Rows[i];

for(int   j=0;j <dt.Columns.Count;j++)
{
oExcel.Cells[i+2,j+1]=dr[dt.Columns[j].ToString()].ToString();
}
}      

wb.Saved   =   true;
filename=     "d:/服务器生成文件/ "+CompanyID.ToString();
if(!System.IO.Directory.Exists(filename))
{
System.IO.Directory.CreateDirectory(filename);
}
oExcel.ActiveWorkbook.SaveCopyAs(filename+ "/ "+DateTime.Now.ToString().Replace( "- ", " ").Replace( "   ", " ").Replace( ": ", " ")+ ".xls ");
oExcel.Quit();
System.GC.Collect();
t.Interval   =   30*1000;
System.Windows.Forms.MessageBox.Show( "成功! ");
}  
}

需要再执行时更改timer的时间间隔   ,但不成功   不知道为什么??

------解决方案--------------------
在t.Interval = 30*1000;后把enable属性改成true
------解决方案--------------------
沒看出來。。
ls的說的不對哦
If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses.
------解决方案--------------------
不能放在外面的,他的好像是每一次循环都新生成一个实例!
class Program
{

static void Main(string[] args)
{
Demo demo = new Demo();
demo.Run();
Console.ReadLine();
}

}
public class Demo
{
public Timer time;
public int count = 0;
public Demo()
{
time = new Ti