每分钟在控制台应用程序中执行代码

问题描述:

我在C#中有一个简单的应用程序,该应用程序将PDF从一个位置移动到另一个位置。每分钟在控制台应用程序中执行代码

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(); 


    } 
} 

}

如果我运行这个程序,它的工作,但是,我需要这个代码,每分钟要执行。我可以使用task scheduler来每分钟运行一次应用程序,但不幸的是,最小运行时间是5分钟。

我试图使用while(true)但它不起作用。如果我在应用程序运行时向文件夹添加更多PDF文件,它将不会将其移至其他文件夹。

我发现了一个建议,在网上使用Timer但我有问题:

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 
    } 

但我得到的编译器错误:

错误2参数1:无法从“诠释”转换到'System.Threading.TimerCallback'C:\ Win \ MoveFiles \ MoveFiles \ MoveFiles \ Program.cs 22 33 MoveFiles

关于如何解决这个问题的任何建议ssue?

+0

有两个Timer类,System.Threading.Timer和System.Timers.Timer。你已经得到了System.Timers.Timer的代码,但是Timer正在解析为System.Threading.Timer。我猜测有一个使用System.Threading,你没有告诉我们。 – 2014-09-05 21:55:30

+0

@mikez,这就是整个应用程序,在这个应用程序中没有更多的代码... – smr5 2014-09-05 21:56:37

+0

在某处使用指令(例如'using System;')。否则,那些对Console,Path,Directory等类的引用将无法编译。 – 2014-09-05 21:58:37

该解决方案比我想象的要容易。

下面是我解决它的方法。这可能不是最好的解决方案,但它适合我的需求。

我创建了一个while循环,并使用Thread.Sleep(60000)强制应用程序在再次执行之前进入睡眠状态。

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); 
     } 

    } 

} 
} 

退房这里的构造函数:http://msdn.microsoft.com/en-us/library/system.threading.timer(v=vs.110).aspx

这是此构造方法签名之一:

Timer(TimerCallback, Object, Int32, Int32) 

你不能实例化一个System.Threading.Timer只间隔。请改用System.Timers.Timer(http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx),或者必须提供TimerCallback,Object状态(可为空),到期时间和句点。请参阅此处:“参数”下的http://msdn.microsoft.com/en-us/library/2x96zfy7(v=vs.110).aspx

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 
} 
+0

它永远不会进入'private static void t_Elapsed(object o)'应用程序关闭...? – smr5 2014-09-05 21:55:05

+0

@ user1426542您需要保持控制台应用程序以某种方式活动。什么时候该关闭? – Zer0 2014-09-05 21:56:34

+0

这个应用程序应该运行至少一天。这是一个将文件从一个位置移动到另一个位置的临时应用程序。它应该在用户关闭应用程序时关闭。 – smr5 2014-09-05 22:00:36