下面是一段定时器使用的代码:
//
开始读取事件
private
void
btRead_Click(
object
sender, System.EventArgs e)

...
{
btRead.Enabled = false;
btStopRead.Enabled = true;
fileClass.CreateDirectory("RecordXmlFolder");
DataSet ds = new DataSet();
DataSet ds1 = new DataSet();
ds = xmlClass.GetFullXml("SettingXmlFolder/SetFolderAndTime.xml","Root");
int count = ds.Tables[0].Rows.Count;
ds1 = ds.Copy();
ds1.Clear();

for(int i=0;i<count;i++)

...{
string folder = ds.Tables[0].Rows[i]["Folder"].ToString();
if(folder.EndsWith(@""))

...{
folder = folder.Substring(0,folder.Length-1);
}
string userName = ds.Tables[0].Rows[i]["UserName"].ToString();
string password = ds.Tables[0].Rows[i]["Password"].ToString();
//连接网络
ConnectRemoteMachine(folder,userName,password);

if(Directory.Exists(folder))

...{
ds1.Tables[0].NewRow();
ds1.Tables[0].Rows.Add(ds.Tables[0].Rows[i].ItemArray);
}
else

...{
MessageBox.Show("共享目录"+folder+"不存在!");
}
}
timer = new System.Threading.Timer[ds1.Tables[0].Rows.Count];
for(int i=0;i<ds1.Tables[0].Rows.Count;i++)

...{

string [] state = new string[] ...{
ds1.Tables[0].Rows[i]["Folder"].ToString(),
ds1.Tables[0].Rows[i]["Type"].ToString()
};

timer[i] = new System.Threading.Timer(new System.Threading.TimerCallback(BeginRead),state,0,Convert.ToInt32(ds.Tables[0].Rows[i]["Time"].ToString()));

}
}

//
定时读取文件
private
void
BeginRead(
object
state)

...
{
lock(this)

...{
string[] arr = (string[])state;

//读取网络文件
GetContents(arr[0],Convert.ToInt32(arr[1]));
}
}
其中:
timer[i]
=
new
System.Threading.Timer(
new
System.Threading.TimerCallback(BeginRead),state,
0
,Convert.ToInt32(ds.Tables[
0
].Rows[i][
"
Time
"
].ToString()));
参数:state是传入定时调用的函数的参数,BeginRead是定时调用的方法名称,