Reputation: 2793
I have a simple application in C# which takes PDF from one location and moves it to another location.
namespace MoveFiles
{
class Program
{
public static string destinationPath = @"S:\Re\C";
static void Main(string[] args)
{
//location of PDF files
string path = @"S:\Can\Save";
//get ONLY PDFs
string[] filePath = Directory.GetFiles(path, "*.pdf");
foreach (string file in filePath)
{
Console.WriteLine(file);
string dest = destinationPath + "\\" + Path.GetFileName(file);
File.Move(file, dest);
}
Console.ReadKey();
}
}
}
If I run this app it does the work, however, I need this code to be executed every minute. I could of used task scheduler
to run the app every minute, but unfortunately the minimum runtime is 5 min.
I have tried to use while(true)
but it doesn't work. If I add more PDF files to the folder while the app is running it will not move it to different folder.
I found a suggestion online to use Timer
but I'm having issues:
static void Main(string[] args)
{
Timer t = new Timer(60000); // 1 sec = 1000, 60 sec = 60000
t.AutoReset = true;
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
t.Start();
}
private static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// do stuff every minute
}
But I get compiler errors:
Error 2 Argument 1: cannot convert from 'int' to 'System.Threading.TimerCallback' C:\Win\MoveFiles\MoveFiles\MoveFiles\Program.cs 22 33 MoveFiles
Any suggestions on how I can resolve this issue?
Upvotes: 3
Views: 17384
Reputation: 2793
The solution was easier than I thought.
Here's how I solved it. It may not be the best possible solution, but it works for my need.
I created a while
loop and used Thread.Sleep(60000)
to force the app to go to sleep before executing again.
namespace MoveFiles
{
class Program
{
public static string destinationPath = @"S:\Re\C";
static void Main(string[] args)
{
//location of PDF files
while (true)
{
string path = @"S:\Can\Save";
//get ONLY PDFs
string[] filePath = Directory.GetFiles(path, "*.pdf");
foreach (string file in filePath)
{
Console.WriteLine(file);
string dest = destinationPath + "\\" + Path.GetFileName(file);
File.Move(file, dest);
}
Thread.Sleep(60000);
}
}
}
}
Upvotes: 3
Reputation: 7354
private static Timer timer;
static void Main(string[] args)
{
timer = new Timer(timer_Elapsed);
timer.Change(60000, 60000);
Console.ReadKey();
}
private static void timer_Elapsed(object o)
{
// do stuff every minute
}
Upvotes: -1
Reputation: 2583
Check out the constructors here: http://msdn.microsoft.com/en-us/library/system.threading.timer(v=vs.110).aspx
This is one of the method signatures for this constructor:
Timer(TimerCallback, Object, Int32, Int32)
You can't instantiate a System.Threading.Timer with just the interval. Either use a System.Timers.Timer instead (http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx) or you have to supply the TimerCallback, Object state (can be null), due time, and period. See here: http://msdn.microsoft.com/en-us/library/2x96zfy7(v=vs.110).aspx under "Parameters."
Upvotes: 0