If you're using Windows.Forms, you can use a Windows.Forms.Timer. Otherwise, the best bet is probably a new thread with a 5 second sleep.
Main Topics
Browse All TopicsI've a c# method that delete files older than 30 seconds. Now I need timer to execute/run this method every 5 seconds. How best can I achieve this? Also I need this application to run at startup
and continue running until shutdown.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
What you need here are 2 steps. The reason for this is that the file attributes can be altered and this can lead to a file not being deleted.
I would create 2 windows services and they should execute the following steps
The first service should implement a filesystemwatcher with which I can log the creation times of the files probably to database, and the 2nd windows service will execute every 5 seconds query the database for any files older then 30 seconds, delete the files and the entries into the database. If you want to keep a history on what was created and deleted then you can keep the entries in the database and just add the deletion time to the affected entries.
By the way you could also implement both steps in one windows service if you whish to. I would prefer 2 as it would be possilble to run them individually and switch them off individualy. Eg you might want to log file creation and not delete files for a period of time. 2 services will allow you to switch of the deletion service and keep on logging. With one servoce you would have to change your code to do that.
Hi there you should be using the System.Timer timer and the elapsed event.
using System.Timers;
Timer deleteTimer = new Timer();
deleteTimer.Elapsed+=new ElapsedEventHandler(delete
protected void deleteTimer_Elapsed(object
{
//Delete files here.
}
After all Windows Forms controls belong on Windows Forms and not windows services as they have no visual component.
To instsall the service you need to firstly add a service installer class. Once you compile your service you can install it using the installutil command in your visual studio command prompt: navigate to the folder where the executable is and then run the following command to install the service
'installutil /i MyService.exe'
to uninstall run command 'installutil /u MyService.exe.
[RunInstaller(true)]
public class WindowsServiceInstaller : Installer
{
/// <summary>
/// Public Constructor for WindowsServiceInstaller.
/// - Put all of your Initialization code here.
/// </summary>
public WindowsServiceInstaller()
{
ServiceProcessInstaller serviceProcessInstaller =
new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
//# Service Account Information
serviceProcessInstaller.Ac
//# Service Information
serviceInstaller.DisplayNa
serviceInstaller.StartType
//# This must be identical to the WindowsService.ServiceBase
//# set in the constructor of WindowsService.cs
serviceInstaller.ServiceNa
this.Installers.Add(servic
this.Installers.Add(servic
}
}
Below is my code but it's not working installed the service successfully and started it successfully but the files are not deleted after 5 seconds. Am I missing something? The problem is the timer, if I place the delete code above the time whenever the script starts the files are deleted but the timer execution is not working.
Yes...
http://dotnetstep.bl
I think your work is done ....
Business Accounts
Answer for Membership
by: jinalPosted on 2009-07-18 at 11:07:37ID: 24886776
Create window service application .
That is best solution if you want to run application in background.
In Window Service you can use System.Timers.Timer class for that purpose.