Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每分钟在控制台应用程序中执行一次代码_C#_Console Application - Fatal编程技术网

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

C# 每分钟在控制台应用程序中执行一次代码,c#,console-application,C#,Console Application,我在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";

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


    }
}
}

如果我运行这个应用程序,它会工作,但是,我需要这段代码每分钟执行一次。我可以使用
任务调度程序
每分钟运行一次应用程序,但不幸的是,最短运行时间是5分钟

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

我在网上找到了使用
定时器的建议,但我遇到了一些问题:

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:无法从“int”转换为“System.Threading.TimerCallback”C:\Win\MoveFiles\MoveFiles\Program.cs 22 33 MoveFiles


关于如何解决此问题,有什么建议吗?

请查看此处的构造函数:

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

Timer(TimerCallback, Object, Int32, Int32)

不能仅使用间隔来实例化System.Threading.Timer。请改用System.Timers.Timer()或提供TimerCallback、对象状态(可以为null)、到期时间和期间。请看这里:在“参数”下。

解决方案比我想象的要简单

我是这样解决的。这可能不是最好的解决方案,但它符合我的需要

我创建了一个
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);
        }

    }

}
}

它永远不会转到
私有静态无效t_appeased(object o)
应用程序关闭…?@user1426542您需要以某种方式保持控制台应用程序的活动状态。应该什么时候关闭?此应用程序应至少运行一天。这是一个临时应用程序,可以将文件从一个位置移动到另一个位置。当用户关闭应用程序时,它应该关闭。@user1426542我编辑了我的答案,以便在用户按键时关闭。我在
timer\u event
中移动了移动文件的代码。现在,在这一行中
timer=新定时器(t_已过)我假设它应该转到
计时器\u expressed
执行代码。。。但它永远不会出现…有两个计时器类,System.Threading.Timer和System.Timers.Timer。您有System.Timers.Timer的代码,但Timer正在解析为System.Threading.Timer。我猜这里有一个using for System.Threading,您还没有给我们看。@mikez,这就是整个应用程序,这个应用程序中没有更多的代码……有些地方有using指令(例如,
using System;
)。否则,对控制台、路径、目录等类的引用将无法编译。我希望对于我自己的项目,我们可以将任务调度器“Repear Task Min”更改为“1 Min”,我正在考虑相同的解决方案。我只是不确定这是否会占用pc上的大量内存?@JorisDecraecker,不幸的是,我不记得它在运行时占用了多少内存,因为我几年前就使用了这段代码,但我认为它的使用量不足以占用机器。当然,在我的例子中,这段代码没有运行超过30分钟。我找到了一个更好的方法来解决这个问题(至少对于我的问题)。我将在批处理程序上使用任务调度程序。如果程序失败,计划程序将在15分钟后再次运行。
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);
        }

    }

}
}