C# 在特定时间执行函数

C# 在特定时间执行函数,c#,winforms,timer,C#,Winforms,Timer,我有一个定时器在我的应用程序中运行,我想停止和启动根据当地时间。 所以我需要这样的东西: if ( time = 08:00) {StartTimer();} If ( time = 18:00) {StopTimer();} //This can be done from the timer event itself 有没有一种方法可以在不使用其他计时器的情况下执行此操作? 我可以从计时器事件本身中停止计时器,但如何再次启动它?您可以尝试以下操作:- 1) 创建一个能满足您需要的控制台应用程

我有一个定时器在我的应用程序中运行,我想停止和启动根据当地时间。 所以我需要这样的东西:

if ( time = 08:00) {StartTimer();}
If ( time = 18:00) {StopTimer();} //This can be done from the timer event itself
有没有一种方法可以在不使用其他计时器的情况下执行此操作? 我可以从计时器事件本身中停止计时器,但如何再次启动它?

您可以尝试以下操作:-

1) 创建一个能满足您需要的控制台应用程序

2) 使用Windows“计划任务”功能,在需要运行控制台应用程序时执行该应用程序

您还可以查看以下示例:-

   using System;
   using System.Threading;

    public class TimerExample {

// The method that is executed when the timer expires. Displays
// a message to the console.
private static void TimerHandler(object state) {

    Console.WriteLine("{0} : {1}",
        DateTime.Now.ToString("HH:mm:ss.ffff"), state);
}

public static void Main() {

    // Create a new TimerCallback delegate instance that 
    // references the static TimerHandler method. TimerHandler 
    // will be called when the timer expires.
    TimerCallback handler = new TimerCallback(TimerHandler);

    // Create the state object that is passed to the TimerHandler
    // method when it is triggered. In this case a message to display.
    string state = "Timer expired.";

    Console.WriteLine("{0} : Creating Timer.",
        DateTime.Now.ToString("HH:mm:ss.ffff"));

    // Create a Timer that fires first after 2 seconds and then every
    // second.
    using (Timer timer = new Timer(handler, state, 2000, 1000)) {

        int period;

        // Read the new timer interval from the console until the
        // user enters 0 (zero). Invalid values use a default value
        // of 0, which will stop the example.
        do {

            try {
                period = Int32.Parse(Console.ReadLine());
            } catch {
                period = 0;
            }

            // Change the timer to fire using the new interval starting
            // immediately.
            if (period > 0) timer.Change(0, period);

        } while (period > 0);
    }

    // Wait to continue.
    Console.WriteLine("Main method complete. Press Enter.");
    Console.ReadLine();
}
}

您可以创建一个每秒滴答作响的线程

在那里,您可以检查是否要启动或停止计时器

请阅读以下内容:

在线程中添加如下内容:

if (CurrentTime == "08:00")
   StartTimer();
else if  if (CurrentTime == "18:00")
   StopTimer();
Thread.Sleep(1000); // Makes the Thread Sleep 1 Second

您可以将计时器的时间间隔设置为14小时,而不是停止或保持其短时间间隔运行,并在内部检查附加条件(一天中的时间)。

由于您需要至少一个计时器始终运行(以检测早上8点的时间),因此您可以只使用一个整天运行的计时器

每当计时器滴答作响时,检查时间。如果不是在0800和1800之间,就什么也不做就回来,等待下一个滴答声


您可以尝试将计时器间隔增加到某个值,例如17:55,然后再次减小,但不会有任何可测量的性能差异,因此这是徒劳的。

好吧,您需要第二个计时器。。但它的问题在哪里?这是你不想使用计时器的唯一原因,因为你不能再次启动它吗?您可以使用第二个计时器在设定的时间启动第一个计时器。我已经有两个计时器,并且担心添加另一个计时器会影响性能。从评论和答案来看,我似乎总是需要至少一个计时器。谁说的?你呢?这不是反对你的问题吗?对不起,我有一个计时器每x秒滴答一次,我需要在18:00停止计时器,并在08:00再次启动。是的,但你可以将18:00后的间隔设置为14小时,然后在下一次迭代中将其更改回原始值。(无论如何,我更愿意检查时间。)谢谢你的回答,但是在我的应用程序中添加一个线程超出了应用程序的范围itself@Josefvz您可以使用调用和委托与应用程序通信。看见