Annunciating the windows service to execute some time consuming task using FileSystemWatcher

人走茶凉 提交于 2019-12-11 14:37:56

问题


in a folder files are saved through code, when new file is created, I need to perform some time consuming job in the background. When performing the job if I receive new file or files in the folder I should not consider that and I need to perform once the job is completed. File creation is annunciation to perform the job, so it is not required to perform the job for every file creation but we should ensure that job is done up to the last annunciation.

I am trying the recursive method to do this, but I am still failing. When new file is created the CallReprocess() method id being executed, my windows service is stopped automatically.

public partial class ReDoService : ServiceBase
{
    Boolean eventFired;
    Boolean reprocessingInProgress;
    String lastCreatedFileName;
public ReDoService()
    {
        InitializeComponent();            

        fileSystemWatcher1.Path = folderWatchPath;
        eventFired = false;
        reprocessingInProgress = false;
        lastCreatedFileName = "";
     }
private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
        {
        lastCreatedFileName = e.FullPath;          
        if (eventFired)
        {
        }
        else
        {
            eventFired = true;
            CallReprocess(lastCreatedFileName);                
        }         
    }
private void CallReprocess(String lastFileName)
    {
        if (!reprocessingInProgress)
        {
            if (lastFileName.ToUpper().Contains("EVENTFIRED"))
            {
                try
                {                      
                    reprocessingInProgress = true;
                    eventFired = false;
                    //// Call the Perform Job method
                    reprocessingInProgress = false;                      
                }
                catch (Exception ex)
                {
                  reprocessingInProgress = true;
                  eventFired = false;
                }
            }
            else
            {
            }
            CallReprocess(lastCreatedFileName);
        }
        else
        {              
        }          
    }

}

来源:https://stackoverflow.com/questions/18789228/annunciating-the-windows-service-to-execute-some-time-consuming-task-using-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!