1、问题描述
程序里需要监视某个目录下的文件变化情况: 一旦目录中出现新文件或者旧的文件被覆盖,程序需要读取文件内容并进行处理。于是使用了下面的代码:
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/dot.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/dot.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
结果发现当一个文件产生变化时,Change事件被反复触发了好几次。这样可能的结果是造成同一文件的重复处理。
2、解决方案:
在Google上进行一番搜索后,得到了下面的一段信息: <<http://www.cnblogs.com/RicCC/archive/2006/12/16/filesystem-watcher.html>>
"...可以参考log4net的做法。通过一个计时器,在文件事件处理中让计时器延迟一段时间之后,再执行加载新的配置文件操作。这样可以避免对文件做一次操作触发了多个更改事件,而多次加载配置文件。"
研究了log4net的代码 - XmlConfigurator.cs,然后参照log4net对代码作了如下改动:
基本思想是使用定时器,在事件触发时开始启动定时器,并记下文件名。当定时器到时,才真正对文件进行处理。
(1). 定义变量
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(2). 初始化FileSystemWatcher和定时器
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/dot.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(3). 文件监视事件触发代码:修改定时器,记录文件名待以后处理
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/dot.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/dot.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(4). 定时器事件触发代码:进行文件的实际处理
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/dot.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/dot.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![点击图片可在新窗口打开](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
附上我的,不是fileSystemWatcher,但是同样是委托执行事件问题的解决方法:
private int TimeoutMillis = 1000; //定时器触发间隔
System.Threading.Timer m_timer = null;
public void form1{
InitializeComponent();
lv_Common.MouseDown += new MouseEventHandler(this.lv_Common_MouseDown);
if (m_timer == null)
{
m_timer = new System.Threading.Timer(new TimerCallback(onMDbClick), null, Timeout.Infinite, Timeout.Infinite);
}
}
/// <summary>
/// 双击某个listview项执行新增操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lv_Common_MouseDown(object sender, MouseEventArgs e)
{
ListViewItem lvt = lv_Common.GetItemAt(e.X, e.Y);
if (lvt != null)
{
if (e.Button == MouseButtons.Left && e.Clicks == 2)
{
m_timer.Change(TimeoutMillis, Timeout.Infinite);
}
}
}
/// <summary>
/// 双击后执行的事件
/// </summary>
/// <param name="state"></param>
private void onMDbClick(object state)
{
MessageBox.Show("click");
}
希望对大家有帮助!