问题
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