关键词不能为空

当前您在: 主页 > 英语 >

FileSystemWatcher触发多次Change事件的解决办法

作者:高考题库网
来源:https://www.bjmy2z.cn/gaokao
2021-02-24 15:04
tags:

-

2021年2月24日发(作者:你在哪里英文)


FileSystemWatcher


触发多次


Change


事件的解决办法








最近要用到


FileSystemWatcher

< br>来监控某个目录中的文


件是否发生改变,如果改变就执行相应的操作。但在开发过


程中,


发现


FileSystemWa tcher


在文件创建或修改后,


会触发


多个


Created



Change d


事件,具体原因就是处理文件的过


程中执行了多次文件系统操 作,触发了多次事件。具体可以


参看微软的关于


FileSys temWatcher


这方面的解释:


Troubleshoo ting FileSystemWatcher Components



另外我在网


上发现



Consolidate Multiple FileSystemWatcher Events


关于


这方面的解决办法,比较实用,方便快速引 入到项目中。



来自


MSDN


的问题说明




Troubleshooting FileSystemWatcher Components



Visual Studio .NET 2003




其他版本此主题尚未评级



-


评价此主题


You may encounter


the following situations while working with the


FileSystemWatcher component:


UNC Path Names Not Accepted on Windows NT 4.0 Computers


If you are working with a FileSystemWatcher component on a


Windows NT version 4.0 computer and trying to set its path to


monitor file system activity on a different Windows NT version


4.0 computer, you will not be able to specify a UNC-based path


value in the Path property to point to the computer in question.


You can only set UNC-based values when working on Windows


2000 computers.


Cannot Watch Windows 95 or Windows 98 Directories


If you set your FileSystemWatcher component to reference a


directory on a Windows 95 or Windows 98 computer, you will


receive an error about an invalid directory path when the project


runs. When using FileSystemWatcher, you cannot watch


directories on computers running Windows 95 or Windows 98.


Multiple Created Events Generated for a Single Action


You may notice in certain situations that a single creation event


generates multiple Created events that are handled by your


component. For example, if you use aFileSystemWatcher


component to monitor the creation of new files in a directory,


and then test it by using Notepad to create a file, you may see


two Created events generated even though only a single file was


created. This is because Notepad performs multiple file system


actions during the writing process. Notepad writes to the disk in


batches that create the content of the file and then the file


attributes. Other applications may perform in the same manner.


Because FileSystemWatcher monitors the operating system


activities, all events that these applications fire will be picked


up.


Note




Notepad may also cause other interesting event


generations. For example, if you use the ChangeEventFilter to


specify that you want to watch only for attribute changes, and


then you write to a file in the directory you are watching using


Notepad, you will raise an event . This is because Notepad


updates theArchived attribute for the file during this operation.


Unexpected Events Generated on Directories


Changing a file within a directory you are monitoring with a


FileSystemWatcher component generates not only a Changed


event on the file but also a similar event for the directory itself.


This is because the directory maintains several types of


information for each file it contains



the names and sizes of


files, their modification dates, attributes, and so on. Whenever


one of these attributes changes, a change is associated with the


directory as well.



解决方案





The .NET framework provides a FileSystemWatcher class that


can be used to monitor the file system for changes. My


requirements were to monitor a directory for new files or


changes to existing files. When a change occurs, the application


needs to read the file and immediately perform some operation


based on the contents of the file.


While doing some manual testing of my initial implementation


it was very obvious that theFileSystemWatcher was firing


multiple events whenever I made a change to a file or copied a


file into the directory being monitored. I came across the


following in the MSDNdocumentation’s Troubleshooting


FileSystemWatcher Components



Multiple Created Events Generated for a Single Action


You may notice in certain situations that a single creation event


generates multiple Created events that are handled by your


component. For example, if you use a FileSystemWatcher


component to monitor the creation of new files in a directory,


and then test it by using Notepad to create a file, you may see


two Created events generated even though only a single file was


created. This is because Notepad performs multiple file system


actions during the writing process. Notepad writes to the disk in


batches that create the content of the file and then the file


attributes. Other applications may perform in the same manner.


Because FileSystemWatcher monitors the operating system


activities, all events that these applications fire will be picked


up.


Note: Notepad may also cause other interesting event


generations. For example, if you use the ChangeEventFilter to


specify that you want to watch only for attribute changes, and


then you write to a file in the directory you are watching using


Notepad, you will raise an event. This is because Notepad


updates the Archived attribute for the file during this operation.



I did some searching and was surprised that .NET did not


provide any kind of wrapper around the FileSystemWatcher to


make it a bit more user friendly. I ended up writing my own


wrapper that would monitor a directory and only throw one


event when a new file was created, or an existing file was


changed.


In order to consolidate the multiple FileSystemWatcher events


down to a single event, I save the timestamp when each event is


received, and I check back every so often (using a Timer) to find


paths that have not caused additional events in a while. When


one of these paths is ready, a single Changed event is fired. An


additional benefit of this technique is that the event from the


FileSystemWatcher is handled very quickly, which could help


prevent its internal buffer from filling up.


Here is the code for a DirectoryMonitor class that consolidates


multiple Win32 events into a single Change event for each


change:


解决方案代码





[csharp] view plaincopyprint?




using System;




using c;




using




using




using




using ing;







namespace ShareReadFile




{








public delegate void FileSystemEvent(String path);











public interface IDirectoryMonitor








{












event FileSystemEvent Change;












void Start();








}











public class DirectoryMonitor : IDirectoryMonitor








{












private readonly FileSystemWatcher


m_fileSystemWatcher = new FileSystemWatcher();












private readonly Dictionary<string, DateTime>


m_pendingEvents = new Dictionary<string, DateTime>();












private readonly Timer m_timer;












private bool m_timerStarted = false;















public DirectoryMonitor(string dirPath)












{
















m_ = dirPath;
















m_eSubdirectories =


false;
















m_d += new


FileSystemEventHandler(OnChange);
















m_d += new


FileSystemEventHandler(OnChange);



















m_timer = new Timer(OnTimeout, null,


te, te);












}















public event FileSystemEvent Change;















public void Start()












{
















m_RaisingEvents =


true;












}















private void OnChange(object sender,


FileSystemEventArgs e)












{
















// Don't want other threads messing with the


pending events right now

















lock (m_pendingEvents)
















{




















// Save a timestamp for the most recent


event for this path





















m_pendingEvents[th] =


;























// Start a timer if not already started





















if (!m_timerStarted)




















{
























m_(100, 100);
























m_timerStarted = true;




















}
















}












}















private void OnTimeout(object state)












{
















List<string> paths;






-


-


-


-


-


-


-


-



本文更新与2021-02-24 15:04,由作者提供,不代表本网站立场,转载请注明出处:https://www.bjmy2z.cn/gaokao/670301.html

FileSystemWatcher触发多次Change事件的解决办法的相关文章

  • 余华爱情经典语录,余华爱情句子

    余华的经典语录——余华《第七天》40、我不怕死,一点都不怕,只怕再也不能看见你——余华《第七天》4可是我再也没遇到一个像福贵这样令我难忘的人了,对自己的经历如此清楚,

    语文
  • 心情低落的图片压抑,心情低落的图片发朋友圈

    心情压抑的图片(心太累没人理解的说说带图片)1、有时候很想找个人倾诉一下,却又不知从何说起,最终是什么也不说,只想快点睡过去,告诉自己,明天就好了。有时候,突然会觉得

    语文
  • 经典古训100句图片大全,古训名言警句

    古代经典励志名言100句译:好的药物味苦但对治病有利;忠言劝诫的话听起来不顺耳却对人的行为有利。3良言一句三冬暖,恶语伤人六月寒。喷泉的高度不会超过它的源头;一个人的事

    语文
  • 关于青春奋斗的名人名言鲁迅,关于青年奋斗的名言鲁迅

    鲁迅名言名句大全励志1、世上本没有路,走的人多了自然便成了路。下面是我整理的鲁迅先生的名言名句大全,希望对你有所帮助!当生存时,还是将遭践踏,将遭删刈,直至于死亡而

    语文
  • 三国群英单机版手游礼包码,三国群英手机单机版攻略

    三国群英传7五神兽洞有什么用那是多一个武将技能。青龙飞升召唤出东方的守护兽,神兽之一的青龙。玄武怒流召唤出北方的守护兽,神兽之一的玄武。白虎傲啸召唤出西方的守护兽,

    语文
  • 不收费的情感挽回专家电话,情感挽回免费咨询

    免费的情感挽回机构(揭秘情感挽回机构骗局)1、牛牛(化名)向上海市公安局金山分局报案,称自己为了挽回与女友的感情,被一家名为“实花教育咨询”的情感咨询机构诈骗4万余元。

    语文