定时器的使用

下面是一段定时器使用的代码:

// 开始读取事件
         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是定时调用的方法名称,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值