请问各位!我做了一个定时器每隔一秒自动执行文件的剪贴操作。
但有时候文件过大,程序一秒钟肯定处理不完,这时候定时器又触发了下一次操作,文件就会被拒绝访问而报错
请问这种应该怎么解决呢
private void diaoYong(object sender, System.Timers.ElapsedEventArgs e)
{
string jcPath = this.zhongZhuanX.Text;
string jsPath = this.jsonBX.Text;
string jsZPath = this.jsonX.Text;
string baojiaPath = this.baoJiaX.Text;
string bujianPath = this.buJianX.Text;
string qianziPath = this.qianZiX.Text;
string tuzhiPath = this.tuZhiX.Text;
string mprPath = this.mprX.Text;
tool.geShi(jcPath,jsPath,jsZPath,baojiaPath,bujianPath,qianziPath,tuzhiPath,mprPath);
}
public void geShi(string jcpath, string jsPath, string jsZPath, string baojiaPath, string bujianPath, string qianziPath, string tuzhiPath,string mprPath)
{
if (DirisNull(jcpath))
{
//取得所有文件名
string[] fi = Directory.GetFiles(jcpath);
//日期
string date1 = "";
for (int j = 0; j < fi.Length; j++)
{
string fName = fi[j].Substring(fi[j].LastIndexOf(@"\") + 1, fi[j].Length - fi[j].LastIndexOf(@"\") - 1);
string ges = fName.Substring(fName.LastIndexOf("."));
date1 = fName.Substring(2, 4);
//JSON备份和炸单 jsPath jsZPath
if (!Directory.Exists(jsPath))
{
Directory.CreateDirectory(jsPath);
}
#endregion
//备份路径
File.Copy(fi[j], jsPath + @"\" + fName, true);
//炸单路径
File.Copy(fi[j], jsZPath + @"\" + fName, true);
File.Delete(fi[j]);
}
}
}
private void SorP_Click(object sender, EventArgs e)
{
string zt = this.SorP.Text;
System.Timers.Timer tm = new System.Timers.Timer(2000);
tm.Elapsed += new System.Timers.ElapsedEventHandler(diaoYong);
tm.AutoReset = true;
if (zt.Equals("开始"))
{
tm.Enabled = true;
this.state.Text = "转存执行中。。。。。。";
this.SorP.Text = "暂停";
this.Form2_Load(this, null);
}
else
{
tm.Enabled = false;
this.state.Text = "转存已暂停。。。。。。";
this.SorP.Text = "开始";
this.Form2_Load(this, null);
}
设置一个全局变量,记住当前是否正在处理,如果发现重入,什么也不做直接返回。