C# 在控制台应用程序c中使用时间小时执行查询#

C# 在控制台应用程序c中使用时间小时执行查询#,c#,C#,我想使用带有参数hour的控制台应用程序截断表。 例如,我想使用系统中的时间在凌晨12点运行querytruncate 这是我在控制台应用程序中使用c#编写的代码 这是行不通的。请给我解决方案或示例代码来解决我的问题。 谢谢…当您运行程序时,它会快速显示时钟,如果不是午夜,它会立即退出。(更准确地说,它打印一些消息并等待按键)。我相信你希望等到午夜。如果准确的时间不那么重要(我的意思是可以接受提前或延迟几秒钟),最简单的解决方案是: static void Main(string[] args)

我想使用带有参数hour的控制台应用程序截断表。 例如,我想使用系统中的时间在凌晨12点运行querytruncate

这是我在控制台应用程序中使用c#编写的代码

这是行不通的。请给我解决方案或示例代码来解决我的问题。
谢谢…

当您运行程序时,它会快速显示时钟,如果不是午夜,它会立即退出。(更准确地说,它打印一些消息并等待按键)。我相信你希望等到午夜。如果准确的时间不那么重要(我的意思是可以接受提前或延迟几秒钟),最简单的解决方案是:

static void Main(string[] args)
{
    Thread.Sleep(DateTime.Today.AddDays(1) - DateTime.Now);
    Console.WriteLine("It's about midnight, I go to sleep");
}

我觉得问题在于代码的执行流程。 你只调用一次代码,然后它就停止了。它现在检查一次并获取当前时间。如果时间是凌晨12:00:00,您可以传递到If语句,但您需要在12:00:00系统时间点运行它,这几乎是不可能的


您应该考虑Windows服务或使用Windows任务管理器:

,如其他答案中所提到的,您不应该使用应用程序来调度任务。看起来您正在执行一项数据库维护任务,所以我要看的第一件事是

1) 查看数据库是否可以安排任务SQL Server Agent例如,可以安排存储过程(或临时SQL)在设定的时间运行,如果您将存储过程(或临时SQL)用于数据库,则可以完成任务

如果您的数据库无法执行此操作,或者您希望执行除截断表之外的其他操作,那么

2) 尝试使用Windows任务计划程序


这可以在设定的时间启动应用程序,易于设置,然后您的应用程序就可以完成它想要做的工作(例如截断表),而不必担心调度问题。

我建议您在代码中做几件事:

  • 不要使用
    string
    比较,而是直接使用
    DateTime
    (查看
    TimeOfDay.TotalSeconds
    )。这样,比较起来就容易多了
  • 使用重复调用,而不仅仅是一个调用(除非您确实可以确保您的程序在12点运行)。通过这种方式,您可以使您的程序更适合您
  • 为了使它更加健壮,请提供一些方法来为您的系统提供容差。这就是自动化应该有多好
  • 例如:

    namespace ConsoleApplication1 {
        class Program {
            static void Main(string[] args) {
    
                while (true) { //read 2. assuming this is to be run forever in this example, but gives also some way to break the loop whenever necessary in your app
                    if (DateTime.Now.TimeOfDay.TotalSeconds <= 1) { //read 1. and 3. this way, you give tolerance of 1 second. Your action will be run on 12:00:00 - 12:00:01
                        Console.WriteLine("Do action to run query truncate");
                        //  //in this line i will execute query truncate.
                        break; //break after saving once, for instance, and run again when the time close... This is to prevent possible multiple executions...
                    }
                    System.Threading.Thread.Sleep(500); //read 2. and 3. check every 500 millisecond, at least to give chance to check the time twice per second
                }
    
                // Keep the console window open in debug mode.
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
            }
        }
    }
    
    命名空间控制台应用程序1{
    班级计划{
    静态void Main(字符串[]参数){
    while(true){//read 2。在本例中,假设这将永远运行,但也提供了一些在应用程序中必要时中断循环的方法
    
    if(DateTime.Now.TimeOfDay.TotalSeconds)您是否尝试在夏普12点(服务器时间)运行应用程序?在服务器上12点时,将运行截短表格的命令一次?您确定没有错过一秒钟吗?可能是您应该更改if条件以检查范围(12.00.01到12.00.05)好的,如果这是你的建议,代码的应用如何?嘿,兰,从你的示例代码“DateTime.Now.TimeOfDay.TotalSeconds”这是一天中的TotalSeconds。如果我想设置为上午8点15分如何?谢谢兰我的名字是伊恩(它是一个“i”,第9个字母),顺便说一句。您也可以使用其他选项,如
    TotalMinutes
    TotalHours
    。您还可以使用
    Minute
    Hour
    Second
    (不计算总数),甚至
    毫秒
    。所有这些都可以在
    DateTime
    类中使用。非常方便。)所以,如果我是你,设置为8:15,我将使用DateTime.Now.Hour==8和DateTime.Now.Minute==15和DateTime.Now.Second,你也可以使用
    DateTime.Now.TimeOfDay
    Hours
    。如果你知道它将是8.15,那么你就知道
    DateTime.Now.TimeOfDay.Hours>=8.25
    (注意.25是小时的分数,也就是15分钟)再加上DateTime.Now.Second谢谢ian…它起作用了。是的,我来自印度尼西亚ian。很高兴认识你ian:)太好了!很高兴你发现它起作用了。。我明白了。。难怪。。那样的话,Selamat Hari Natal!=圣诞快乐!
    namespace ConsoleApplication1 {
        class Program {
            static void Main(string[] args) {
    
                while (true) { //read 2. assuming this is to be run forever in this example, but gives also some way to break the loop whenever necessary in your app
                    if (DateTime.Now.TimeOfDay.TotalSeconds <= 1) { //read 1. and 3. this way, you give tolerance of 1 second. Your action will be run on 12:00:00 - 12:00:01
                        Console.WriteLine("Do action to run query truncate");
                        //  //in this line i will execute query truncate.
                        break; //break after saving once, for instance, and run again when the time close... This is to prevent possible multiple executions...
                    }
                    System.Threading.Thread.Sleep(500); //read 2. and 3. check every 500 millisecond, at least to give chance to check the time twice per second
                }
    
                // Keep the console window open in debug mode.
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
            }
        }
    }