FileSystem观察程序包含多个文件 [英] FileSystem watcher with multiple files

查看:68
本文介绍了FileSystem观察程序包含多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在使用FileSystem Watcher时遇到了一些问题。这会在目录带中读取一些filr调用另一个进程,但现在它正在多次调用进程.IF 10文件马上就来了,我在窗口服务中实现的FileSystemWatcher为每个文件调用外部进程10次。



请指导这个问题?





Hi,

I have some problem withFileSystem Watcher.This reads some filr in directory band call another process but now it is calling process multiple times.IF 10 files comes at once,this FileSystemWatcher I implemeted in window service calls external process10 times for each file.

Please guide on this Problem?


        protected override void OnStart(string[] args)
        {

             current_directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
          
           


                strDir = ConfigurationManager.AppSettings["Directory"];
                fileMask = ConfigurationManager.AppSettings["FileMask"];
                strBatfile = ConfigurationManager.AppSettings["Batch"];
                strlog = ConfigurationManager.AppSettings["Log"];

                m_Watcher = new FileSystemWatcher();


                m_Watcher.Filter = fileMask;
                m_Watcher.Path = strDir + "\\";
                m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                 | NotifyFilters.FileName | NotifyFilters.DirectoryName;




                m_Watcher.Created += new FileSystemEventHandler(OnCreated);

                m_Watcher.Deleted += new FileSystemEventHandler(OnDeleated);
                m_Watcher.Renamed += new RenamedEventHandler(OnRenamed);


                m_Watcher.EnableRaisingEvents = true;
            
           

        }
       

        private static void OnCreated(object source, FileSystemEventArgs e)
        {


            Log.getLogger("File Created- Filename :" + e.Name + " at timestamp : " + DateTime.Now.ToString(), strlog);
            string strFileExt = e.Name;


            var extension = Path.GetExtension(strFileExt).ToUpper();


            try
            {
                


                    Log.getLogger("File Processed after executing batch:  Filename ->:" + e.Name + " " + "Batch File Executed- > " + strBatfile + " at timestamp : " + DateTime.Now.ToString(), strlog);

                

            }
            catch (Exception exception)
            {

                CustomException.Write(CustomException.CreateExceptionString(exception, strFileExt));
            }
            finally
            {
                m_Watcher.EnableRaisingEvents = true;
            }
        }

       

        protected override void OnStop()
        {
            string alert = "Win service stopped ";
            CustomException.Write(CustomException.CreateExceptionString(alert));
           
        }
    }
}

推荐答案

FileSystemWatcher提升是正常的文件副本的多个事件。通常至少会引发一个create和一个change事件,而且可能会有更多。如果一个防病毒程序正在运行,可能会有更多。



10是不寻常的,但我没有看到你的代码有任何问题,每个文件只引发三个事件在我的系统上。
It is normal for FileSystemWatcher to raise multiple events for file copies. Generally at least one create, and one change event is raised, plus there might be more. If an antivirus program is running there can be even more.

10 is unusual, but I don't see any issue with your code which only raises three events per file on my system.


static ConcurrentQueue<string> filenames = new ConcurrentQueue<string>();

    static void QueueHandler()
    {
        bool run = true;
        AppDomain.CurrentDomain.DomainUnload += (s, e) =>
        {
            run = false;
            filenames.Enqueue("stop");
        };
        while(run)
        {
            string filename;
            if (filenames.TryDequeue(out filename) && run)
            {
                var proc = new Process();
                proc.StartInfo.FileName = filename;
                proc.Start();
                proc.WaitForExit(); // this blocks until the process ends....

            }
        }
    }




Now we need a single Task/Thread that will run QueueHandler and our   FileSystemWatcher:







protected override void OnStart(string[] args)
{
        // have our queue reader method started
        Task.Factory.StartNew(QueueHandler);

        var fsw = new FileSystemWatcher();
        fsw.Created += (o, e) =>
            {
                // add a file to the queue
                filenames.Enqueue(e.FullPath);
                // optionally add polling for missed files
                // http://stackoverflow.com/questions/239988/filesystemwatcher-vs-polling-to-watch-for-file-changes
            };

        fsw.Path = ConfigurationManager.AppSettings["Directory"];
        fsw.NotifyFilter = NotifyFilters.FileName;
        fsw.Filter = ConfigurationManager.AppSettings["FileMask"];

        fsw.EnableRaisingEvents = true;
}


这篇关于FileSystem观察程序包含多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆