线程睡眠和Windows服务 [英] Thread Sleep and Windows Services

查看:122
本文介绍了线程睡眠和Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Windows服务这是使用以 Thread.sleep代码的一些问题(),所以我想我会尝试用一个定时器,而不是因为这个问题的建议:

I'm working on a Windows Service that's having some issues with Thread.Sleep() so I figured I would try to use a timer instead as this question recommends:

<一个href="http://stackoverflow.com/questions/998142/using-thread-sleep-in-a-windows-service">http://stackoverflow.com/questions/998142/using-thread-sleep-in-a-windows-service

的是它并不完全清楚,我怎么一会实现这一点。我相信这是这样的,但我只是想确认:

Thing is it's not entirely clear to me how one might implement this. I believe this is the way but I just wanted to make sure:

'' Inside The Service Object
Dim closingGate As System.Threading.AutoResetEvent

Protected Overrides Sub OnStart(ByVal args() As String)
   Dim worker As New Threading.Thread(AddressOf Work)
   worker.Start()
End Sub

Protected Sub Work()
   Dim Program = New MyProgram()
   closingGate = New System.Threading.AutoResetEvent(False)
   Program.RunService(closingGate)
End Sub

Protected Overrides Sub OnStop()
   closingGate.Set()
End Sub

'' Inside My Programs Code:
Public Sub RunService(closingGate As AutoResetEvent)
   Dim tmr As New Timer
   '' ...and so on

   closingGate.WaitOne()
End Sub

除了使用VB.Net(开玩笑,但我会使用C#,如果我可以。)我是在正确的轨道吗?这是比使用的Thread.Sleep()

推荐答案

老实说,我不得不说,我觉得你有点过这里的老路。

Honestly, I would have to say that I think you're a little off the beaten path here.

首先,实际的code发布并没有真正意义;工作线程正在等待一个信号,但没有理由 - 它实际上不是在任何类型的循环或等待资源。第二,如果你确实需要接收关闭信号后,执行一些(或许略)清理code的工人,你的服务可能不会给工作线程足够的时间来清理。

First of all, the actual code posted doesn't really make sense; the worker thread is waiting for a signal but for no reason - it's not actually in a loop of any kind or waiting for a resource. Second, if you did need to execute some (perhaps omitted) cleanup code in the worker after receiving the shutdown signal, your service might not give the worker thread enough time to clean up.

但是,更重要的是,你所做的一切就是将问题一个单独的线程。这可以保持服务响应于服务控制器,但它不能解决设计问题 - 它通过使用线程和互斥增加了很多不必要的复杂性;它最终只会让你的服务难以调试,如果你以往任何时候都需要。

But, more fundamentally, all you've done is move the problem to a separate thread. This may keep the service responsive to the service controller but it doesn't fix the design problem - and it adds a lot of unnecessary complexity by using the threads and mutexes; ultimately it's just going to make your service harder to debug if you ever need to.

比方说,你需要执行一些code每5秒。我们的想法是,而不是等待5秒,可以反控制,并让计时器调用你的工作。

Let's say you need to execute some code every 5 seconds. The idea is, instead of "waiting" 5 seconds, you invert the control, and let the timer invoke your work.

这是不好的:

protected override void OnStart(string[] args)
{
    while (true)
    {
        RunScheduledTasks();    // This is your "work"
        Thread.Sleep(5000);
    }
}

这是很好的:

public class MyService : ServiceBase
{
    private Timer workTimer;    // System.Threading.Timer

    protected override void OnStart(string[] args)
    {
        workTimer = new Timer(new TimerCallback(DoWork), null, 5000, 5000);
        base.OnStart(args);
    }

    protected override void OnStop()
    {
        workTimer.Dispose();
        base.OnStop();
    }

    private void DoWork(object state)
    {
        RunScheduledTasks();  // Do some work
    }
}

就是这样。这就是你需要做定期做的工作。只要工作本身不为秒一次运行,服务将继续响应于服务控制器。你甚至可以支持在这种情况下暂停:

That's it. That's all you have to do to do work at regular intervals. As long as the work itself doesn't run for seconds at a time, your service will remain responsive to the service controller. You can even support pausing under this scenario:

protected override void OnPause()
{
    workTimer.Change(Timeout.Infinite, Timeout.Infinite);
    base.OnPause();
}

protected override void OnContinue()
{
    workTimer.Change(0, 5000);
    base.OnContinue();
}

当你需要开始在你的服务创建工作线程的唯一时间是在工作本身可以运行了很长的时间,你需要取消中间的能力。这往往涉及了大量的设计工作,所以我不会进入,如果不知道是肯定的,它的有关您的方案。

The only time when you need to start creating worker threads in your service is when the work itself can run for a very long time and you need the ability to cancel in the middle. That tends to involve a good deal of design effort, so I won't get into that without knowing for sure that it's related to your scenario.

最好不要开始引入多线程语义到你的服务,除非你真的需要它。如果你只是试图安排工作的小十岁上下的单位工作要做,你肯定不需要它。

It's best not to start introducing multithreaded semantics into your service unless you really need it. If you're just trying to schedule small-ish units of work to be done, you definitely don't need it.

这篇关于线程睡眠和Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆