kbuss
asked on
C# Windows Service Timer stopping
I have a C# windows service which uses a timer to check for files in a directory every second. If files exit then the timer_elapsed method stops the timer, processes the files and upon completion restarts the timer. The process then starts again.
The problem I am facing is that every now and then the timer does not work. The service is still running. I know that it is not a problem with the files as I have put them through outside of this project and they work. So I believe the issue is with the timer itself. It's very difficult to actually test for this because the timer just doesn't restart.
Here is my example code:
I was hoping somewhere someone can give me an idea on what the problem I am facing is and possible steps I need to resolve it.
Thank you.
The problem I am facing is that every now and then the timer does not work. The service is still running. I know that it is not a problem with the files as I have put them through outside of this project and they work. So I believe the issue is with the timer itself. It's very difficult to actually test for this because the timer just doesn't restart.
Here is my example code:
private static System.Timers.Timer _timer;
_timer = new System.Timers.Timer();
_timer.Interval = 1000;
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
_timer.Start();
GC.KeepAlive(_timer);
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { FileChecker();
}
public void FileChecker()
{
_timer.Stop();
string fileExtensions = "*.txt";
DirectoryInfo myDirInfo = new DirectoryInfo(@"C:\Program Files (x86)\MyProgram\Process\");
for (int i = 0; i <= fileExtensions.GetUpperBound(0); ++i)
{
FileInfo[] allFiles = myDirInfo.GetFiles(fileExtension);
if (allFiles.Any())
{
foreach (FileInfo f in allFiles)
{
string filePath = Convert.ToString(myDirInfo + f.ToString());
FileReader myFileReader = new FileReader ();
myFileReader .SetFilePath = filePath;
myFileReader .ReadFile();
File.Delete(filePath);
}
}
}
_timer.Start();
}
I was hoping somewhere someone can give me an idea on what the problem I am facing is and possible steps I need to resolve it.
Thank you.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Ok the whole antivirus thing was a load of old bong. I knew it was, just was a coincidence. I was clutching at straws.
Right, the try, finally seems to be what I'am looking for. It has forced my timer to restart and therefore keeps the program cycle working.
Now I have a dilemma and would greatly appreciate any opinions. I have two practically identical programs. One is a windows service (the final program), one is just a C# foms app..
I drop my 2000 files into lets say my spool folder in the forms app. It processes the lot without any exceptions, run time errors etc.... I do the same in the windows service which has a timer set to check the folder every 1 sec to see if anything is in it. Everything else is the same yet the timer seems to stop every now and then. So I stop/start the service and the process carrys on until the next interupt.
The try, finally resolves this problem. Though it feels like I'm essentially kicking the can down the road. There is still an underlying problem somewhere causing the timer to stop.
However, as previously mentioned the code and classes run fine in the forms app. So unlikely to be the code directly. Although the try, finally resolving the issues seems to insinuate there is a problem within the try which is forcing the timer to stop.
This is a real headache. From the information I have given you would you say that the problem is with my code or something external?
Right, the try, finally seems to be what I'am looking for. It has forced my timer to restart and therefore keeps the program cycle working.
Now I have a dilemma and would greatly appreciate any opinions. I have two practically identical programs. One is a windows service (the final program), one is just a C# foms app..
I drop my 2000 files into lets say my spool folder in the forms app. It processes the lot without any exceptions, run time errors etc.... I do the same in the windows service which has a timer set to check the folder every 1 sec to see if anything is in it. Everything else is the same yet the timer seems to stop every now and then. So I stop/start the service and the process carrys on until the next interupt.
The try, finally resolves this problem. Though it feels like I'm essentially kicking the can down the road. There is still an underlying problem somewhere causing the timer to stop.
However, as previously mentioned the code and classes run fine in the forms app. So unlikely to be the code directly. Although the try, finally resolving the issues seems to insinuate there is a problem within the try which is forcing the timer to stop.
This is a real headache. From the information I have given you would you say that the problem is with my code or something external?
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Cheers.
Also, I was thinking you might be interested in FileSystemWatcher:
http://msdn.microsoft.com/ en-us/libr ary/system .io.filesy stemwatche r.aspx
This would allow you to receive events whenever a file is added to a directory for instance so no need to poll at regular intervals with a Timer. Just a suggestion but it might be a better way to handle your code :-)
http://msdn.microsoft.com/
This would allow you to receive events whenever a file is added to a directory for instance so no need to poll at regular intervals with a Timer. Just a suggestion but it might be a better way to handle your code :-)
ASKER