Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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# 如何在windows服务中停止计时器,使代码不';你不会打多次吗?_C#_Timer_Windows Services - Fatal编程技术网

C# 如何在windows服务中停止计时器,使代码不';你不会打多次吗?

C# 如何在windows服务中停止计时器,使代码不';你不会打多次吗?,c#,timer,windows-services,C#,Timer,Windows Services,这是示例控制台应用程序,而不是原始WINDOWS服务 我有以下代码(仅示例代码)。我希望我的计时器只执行一次,然后停止 目前我正在使用count字段检查代码是否已命中。它工作的很好,但我想优化的东西 注意: 使用计时器的原因是,在安装windows serviceOnStart期间,事件不应延迟服务安装过程 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys

这是示例控制台应用程序,而不是原始WINDOWS服务

我有以下代码(仅示例代码)。我希望我的计时器只执行一次,然后停止

目前我正在使用
count
字段检查代码是否已命中。它工作的很好,但我想优化的东西

注意: 使用计时器的原因是,在安装windows service
OnStart
期间,事件不应延迟服务安装过程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    static class Program
    {
        static int count = 0;

        static void Main()
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Elapsed += OnElapseTime;
            timer.Interval = TimeSpan.FromSeconds(5).TotalMilliseconds;
            timer.Start();
            System.Threading.Thread.Sleep(30000);
            timer.Stop();
        }

        static void OnElapseTime(object sender,
                                          System.Timers.ElapsedEventArgs e)
        {
            if (count < 1)
            {
                count++;
                Console.WriteLine("Timer ticked...");
                Console.ReadLine();
            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序1
{
静态类程序
{
静态整数计数=0;
静态void Main()
{
System.Timers.Timer Timer=新的System.Timers.Timer();
timer.appeated+=一次失效时间;
timer.Interval=TimeSpan.FromSeconds(5).total毫秒;
timer.Start();
系统。线程。线程。睡眠(30000);
timer.Stop();
}
静态无效时间(对象发送方,
System.Timers.ElapsedEventArgs(第e部分)
{
如果(计数<1)
{
计数++;
控制台。WriteLine(“计时器滴答作响…”);
Console.ReadLine();
}
}
}
}

有多种方法可以实现您想要的,本质上就是延迟执行

修改现有内容的最简单方法可能是:

static void Main()
{
   System.Timers.Timer timer = new System.Timers.Timer();
   timer.Elapsed += OnElapseTime;
   timer.Interval = TimeSpan.FromSeconds(5).TotalMilliseconds;
   timer.Start();
}

static void OnElapseTime(object sender, System.Timers.ElapsedEventArgs e)
{
    var timer = (System.Timers.Timer)sender;
    timer.Stop();  // stop the timer after first run

    // do the work.. (be careful, this is running on a background thread)
    Console.WriteLine("Timer ticked...");
    Console.ReadLine();            
} 
另一种方法可能是使用System.Threading.Timer类而不是System.Timers.Timer,并计划一次延迟执行:

static void Main()
{
   var timer = new System.Threading.Timer(RunDelayed, null, imeSpan.FromSeconds(5).TotalMilliseconds, Timeout.Infinite );
}

private void Callback( Object state )
{
    // do the work.. (be careful, this is running on a background thread)
    Console.WriteLine("Timer ticked...");
    Console.ReadLine(); 
}
下面是一篇讨论.NET中不同计时器类之间差异的好文章:

只需将设置为
false
。这会导致事件只触发一次。您可以使用
count
删除代码

timer.AutoReset = false;

问题是什么?如何停止计时器,使
OnElapseTime
不再调用。它应该只调用一次。如果计时器只应该执行一次,为什么要使用它?只需执行一个新线程或任务。@Bauss,OP想在执行代码之前等待一段时间。
task.Delay
有人吗?谢谢,这是甜蜜而简单的