C# 如何在正确的时间下订单#

C# 如何在正确的时间下订单#,c#,C#,首先对不起我的英语不好 我想在我的电脑上12:00时在我的程序中下订单 我尝试使用此代码 string Time = "00:00 AM"; while (true) { if (Time == DateTime.UtcNow.ToString("hh:mm tt")) { Console.ForegroundColor = ConsoleColor.Blue; // Update Days

首先对不起我的英语不好

我想在我的电脑上12:00时在我的程序中下订单 我尝试使用此代码

    string Time = "00:00 AM";
    while (true)
    {
        if (Time == DateTime.UtcNow.ToString("hh:mm tt"))
        {
            Console.ForegroundColor = ConsoleColor.Blue;

            // Update Days -1 Where Service = 1
            sqlCon.exec("update HelperSystem set Days = Days-1 where Service=1 and Days != 0");
            Meldung("Updated HelperSystem Table Days -1");

            // Update Service = 0 Where Days = 0
            sqlCon.exec("update HelperSystem set Service = 0 where Days = 0");
            Meldung("Updated HelperSystem Table Service = 0 Where Days = 0");



            // Update Days -1 Where Service = 1
            sqlCon.exec("update AutoEvent set Days = Days-1 where Service=1 and Days != 0");
            Meldung("Updated AutoEvent Table Days -1");

            // Update Service = 0 Where Days = 0
            sqlCon.exec("update AutoEvent set Service = 0 where Days = 0");
            Meldung("Updated AutoEvent Table Service = 0 Where Days = 0");

            Console.ResetColor();
            Thread.Sleep(120000);
        }
        Thread.Sleep(1);
    }
当我尝试使用断点返回值12:00AM但没有执行任何操作时

更改字符串时间=“4:30AM”;至“凌晨4:30”


如果您试图使用格式化字符串匹配时间,则应确保与相同格式进行比较。你应该这样做:

string timeFormat = "hh:mm tt";
string Time = (new DateTime(1970, 1, 1, 0, 0, 0)).ToString(timeFormat); //12:00 AM
while (true)
{
    if (Time == DateTime.UtcNow.ToString(timeFormat))
    {
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("Run.");
        Console.ResetColor();
        Thread.Sleep(120000);
    }
    Thread.Sleep(1);
}
然而,这种方法并不好。你真的没有办法确保过程在一天中正确的时间执行,以确保平等。在这种情况下,您将非常不幸运,因为它只需在60秒内运行一次时间文本是否正确,但无法保证。你真的应该和一个特定的时间进行比较

这样更好:

DateTime Time = DateTime.UtcNow.AddDays(1.0).Date;
while (true)
{
    if (DateTime.UtcNow > Time)
    {
        Time = DateTime.UtcNow.AddDays(1.0).Date;
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("Run.");
        Console.ResetColor();
    }
    Thread.Sleep(1);
}
没有必要使用凌乱的
线程.Sleep(120000)在此代码中

但是,您仍然有
线程Sleep(1)呼叫错误

我建议使用专门为这类事情设计的库。尝试Microsoft的反应式框架(NuGet“System.Reactive”),然后您可以执行以下操作:

IDisposable subscription =
    Observable
        .Timer(DateTimeOffset.UtcNow.AddDays(1.0).Date, TimeSpan.FromDays(1.0))
        .Subscribe(x =>
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("Run.");
            Console.ResetColor();
        });

关闭应用程序时,只需调用
subscription.Dispose(),它将完全停止。

除非您的程序设计为始终运行,否则我建议您使用Windows的计划任务。这更有意义。如果应用程序总是在运行,那么当存在计时器和异步时,while(true)循环并不是实现这一点的最佳方法。另外,你打算使用UtcNow还是你的本地时间?@john它是一个控制台应用程序,是的,它在VPS上全天候工作,我尝试使用Datetime。现在,不要做任何事情。如果它打算像你说的那样一直运行,这种方法是可行的,尽管我建议比较时间,决定你是否可以睡更长的时间,这样可以节省一些额外的CPU周期。编辑:经过考虑,我认为你最好使用这样的解决方案-比较字符串是个坏主意,甚至比较日期部分中的日期时间也更好。在这种情况下,比较不起作用,因为“hh”将在凌晨4点输出“04”,而不是“4”。您想将其与格式为“h:mm tt”的日期进行比较。我建议您查看并最终了解代码所做操作的含义。我在添加帖子之前尝试了一些内容抱歉,我将帖子编辑为凌晨00:00
IDisposable subscription =
    Observable
        .Timer(DateTimeOffset.UtcNow.AddDays(1.0).Date, TimeSpan.FromDays(1.0))
        .Subscribe(x =>
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("Run.");
            Console.ResetColor();
        });