zoukankan      html  css  js  c++  java
  • C# FileSystemWatcher 并发

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.IO;
    using System.Collections;

    namespace Demo
    {
        public class FileListen
        {
            private AutoResetEvent autoResetEvent;
            private string listenPath = @"D:项目资料随笔Demolisten";
            private Queue fileQueue = new Queue();
            private static object objLock = new object();
            private bool isWait = true;

            public FileListen(string listenPath)
            {
                this.listenPath = listenPath;
                Init();
            }

            private void Init()
            {
                FileSystemWatcher watcher = new FileSystemWatcher(listenPath);
                watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
                watcher.Filter = "*.txt";
                watcher.EnableRaisingEvents = true;
                watcher.Created += new FileSystemEventHandler(MonitorFileCreate);

                autoResetEvent = new AutoResetEvent(false);

                Thread thread = new Thread(DoWork);
                thread.Start();

                Reload();
            }

            private void Reload()
            {
                DirectoryInfo dires = new DirectoryInfo(listenPath);
                FileInfo[] files = dires.GetFiles().OrderBy(c => c.CreationTime).ToArray();
                foreach (FileInfo file in files)
                {
                    if (!fileQueue.Contains(file.FullName))
                    {
                        fileQueue.Enqueue(file.FullName);
                        //休眠1秒
                        Thread.Sleep(1000);
                        autoResetEvent.Set();
                    }
                }
            }

            private void DoWork()
            {
                Console.WriteLine("DoWork Begin");
                //等待信号
                while (isWait)
                {
                    autoResetEvent.WaitOne();
                    //锁定 防止并发
                    lock (objLock)
                    {
                        //获取文件队列信息 并移除
                        string path = fileQueue.Dequeue().ToString();
                        Console.WriteLine("正在处理=>" + path);
                        Console.WriteLine("处理中...");
                        Console.WriteLine("处理完成");
                        File.Delete(path);
                    }
                }
            }

            private void MonitorFileCreate(object sender, FileSystemEventArgs e)
            {
                if (e.ChangeType == WatcherChangeTypes.Created)
                {
                    try
                    {
                        if (!fileQueue.Contains(e.FullPath))
                        {
                            Console.WriteLine("监听文件=>" + e.FullPath);

                            Thread.Sleep(500);
                            fileQueue.Enqueue(e.FullPath);
                            autoResetEvent.Set();
                        }
                    }
                    catch (Exception ex)
                    {
                        //LogHelper.Error(ex.Message + " " + ex.StackTrace);
                    }
                }
            }
        }
    }

  • 相关阅读:
    刷题--两个链表生成相加链表
    机器学习与模式识别之——组合模型
    阅读笔记--CSI fingerprinting with SVM regression to achieve device-free passive localization
    复制含有随机指针节点的链表
    将数组排列成左边小,中间相等,右边大的形式 给定链表节点数组和某个值
    EDA(Experimental Data Analysis)之常见分析方法总结--以kaggle的泰坦尼克号之灾为例
    Data Analysis with Python : Exercise- Titantic Survivor Analysis | packtpub.com
    ubuntu16.04配置搜狗输入法
    用栈来求解hanoi塔问题
    codeforces 792 B. Counting-out Rhyme 约瑟夫环
  • 原文地址:https://www.cnblogs.com/zeshao/p/9019925.html
Copyright © 2011-2022 走看看