在Filewatcher窗口服务中进行线程化

时间:2014-12-16 14:20:12

标签: c# .net service

我编写了一个窗口服务,它使用Filewatcher监视特定文件夹中文件的创建。但是,我的问题是在真实环境中,许多文件(大约20-30个)都出现在生产环境的文件夹中。如何我处理如此庞大的数据文件。我需要实现线程或其他东西。对于线程,任何示例代码都将受到赞赏,因为我不知道该怎么做。 代码如下:

protected override void OnStart(string[] args)
{        
    XmlDocument xml = new XmlDocument();
    xml.Load("C:\\Users\\\\Data.xml");

    XmlNodeList xnList = xml.SelectNodes("/Names/Name");
    foreach (XmlNode xn in xnList)
    {
        strDir = xn["Directory"].InnerText;
        fileMask = xn["FileMask"].InnerText;
        strBatfile = xn["Batch"].InnerText;
        strlog = xn["Log"].InnerText; 
    }

    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
    {
        if (extension == ".txt")
        {
            string MyBatchFile = strBatfile;
            string parameterFilename = strFileExt;
            System.Diagnostics.Process.Start(MyBatchFile);

            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;
    }
}

问题2 -

  

用Onstop()方法编写的最佳实践是什么。

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方法,所以想发布它。

    protected override void OnStart(string[] args)
    {

         current_directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        XmlDocument xml = new XmlDocument();
        try
        {
              string Xml_Path = System.AppDomain.CurrentDomain.BaseDirectory;
             xml.Load(current_directory+"\\Data.xml");//suppose that myXmlString contains "<Names>...</Names>"


                XmlNodeList xnList = xml.SelectNodes("/Names/Name");
                 foreach (XmlNode xn in xnList)
                 {
                     strDir = xn["Directory"].InnerText;
                     fileMask = xn["FileMask"].InnerText;
                     strBatfile = xn["Batch"].InnerText;
                     strlog = xn["Log"].InnerText;


                 }

            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;
        }
        catch (Exception exception)
        {
            CustomException.Write(CustomException.CreateExceptionString(exception.ToString()));
        }

    }