I am working on a windows service which will look for backup files in a particular folder. When it finds one the service will move all the backup files from that location and move them to an archived folder.
I have used FileSystemWatcher
before but since it doesn't work on Servers I am using DirectoryInfo
to look for the files.
The requirement is to run this service at every 5 minutes interval to look for any new backup files coming in.
I am stuck with the timer implementation.
I want to call MoveToProcessed(processed)
method from the ElapsedEventHandler. but I am getting error CS0149 - Method name expected.
protected override void OnStart(string[] args)
{
DirectoryInfo dir = new DirectoryInfo(backupdirectory);
// Other unrelated code omitted
// Move all the Backup files to Processed folder at certain intervals.
createOrderTimer = new System.Timers.Timer();
//***** ERROR ON THE FOLLOWING LINE *****
createOrderTimer.Elapsed += new ElapsedEventHandler(MoveToProcessed(processed));
//***************************************
createOrderTimer.Interval = 300000; // 15 min
createOrderTimer.Enabled = true;
createOrderTimer.AutoReset = true;
createOrderTimer.Start();
}
private void MoveToProcessed(string processed)
{
// Code here backs up and restores files
}
You can easily call a method from an event handler, but the event handler method itself must match a specific signature. For the System.Timers
class, it should look something like this:
private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
So you can simply create a method with this signature, assign it as an event handler, and then call your method from there:
// Elapsed event handler calls our other method
private static void CreateOrderTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// Not sure what the string argument represents, or how it should be set
MoveToProcessed("some string");
}
// We can assign the event handler above when creating the Timer:
protected override void OnStart(string[] args)
{
var createOrderTimer = new System.Timers.Timer
{
Interval = TimeSpan.FromMinutes(15).TotalMilliseconds,
AutoReset = true
};
createOrderTimer.Elapsed += CreateOrderTimer_Elapsed;
createOrderTimer.Start();
}