简体   繁体   English

C#Timer与服务中的线程

[英]C# Timer vs Thread in Service

I have a Service that hits a database every 10 sec and gets the data if there is any. 我有一个服务,每隔10秒就会访问一个数据库并获取数据(如果有的话)。 The thing is that processing this data can take up to 30 sec. 问题是处理这些数据最多可能需要30秒。 If I use a Timer with 10 sec interval the service will get the same data twice. 如果我使用间隔10秒的定时器,服务将获得两次相同的数据。

The effect i´m trying to achieve(Just for visualization): 我想要实现的效果(仅用于可视化):

while(true)
{
    if(Getnrofrows() > 0)
      do stuff
    else
      sleep for 10 sec
}

Ppl saying Thread.Sleep is a bad idea in production services, how do I do this with timers? Ppl说Thread.Sleep在生产服务中是个坏主意,我该怎么做定时器呢?

/mike /麦克风

Did you try to set Timer property auto reset to false, and enabling timer again when process of refreshing data is over 您是否尝试将Timer属性自动重置设置为false,并在刷新数据的过程结束时再次启用计时器

using System;

public class PortChat
{
    public static System.Timers.Timer _timer;
    public static void Main()
    {

        _timer = new System.Timers.Timer();
        _timer.AutoReset = false;
        _timer.Interval = 100;
        _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
        _timer.Enabled = true;
        Console.ReadKey();
    }

    static void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        //Do database refresh
        _timer.Enabled = true;
    }
}

I don't see any problems with using Sleep at all other than you might end up with ugly code. 我没有看到使用Sleep的任何问题,除了你可能最终得到丑陋的代码。

To answer your question: 回答你的问题:

public class MyTest
{
    System.Threading.Timer _timer;


    public MyTest()
    {
       _timer = new Timer(WorkMethod, 15000, 15000);
    }


    public void WorkMethod()
    {
       _timer.Change(Timeout.Infinite, Timeout.Infinite); // suspend timer

       // do work

       _timer.Change(15000, 15000); //resume

    }
}

There is nothing wrong with this approach. 这种方法没有错。 A sleeping thread does not consume any CPU cycles. 休眠线程不消耗任何CPU周期。

If you need to do something exactly every X seconds, a timer is the way to go. 如果你需要每隔X秒做一些事情,那么计时器就是你要走的路。 If, on the other hand, you want to pause for X seconds, then Thread.Sleep is appropriate. 另一方面,如果要暂停X秒,则Thread.Sleep是合适的。

Thread.Sleep is not bad in itself in a service, just that you need to be responsive to service commands, so your worker thread should not go to sleep for an hour, but rather needs to sleep for short periods of time and then wake up and listen if the service controller part of the service is telling it to stop for some reason. Thread.Sleep本身在一个服务中并不坏,只是你需要响应服务命令,所以你的工作线程不应该睡一小时,而是需要在很短的时间内睡觉然后醒来并监听服务的服务控制器部分是否因某种原因告诉它停止。

You want to make it so that if the admin tells your service to stop, it'll stop quickly enough so that it won't get any timeout messages where the admin can't be sure that your service is stopped and it's safe to reboot the machine or similar. 您希望这样做,以便如果管理员告诉您的服务停止,它将足够快地停止,以便它不会收到任何超时消息,其中管理员无法确定您的服务已停止并且可以安全地重新启动机器或类似的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM