每x秒调用/启动Windows服务的最佳方法是什么? [英] What is the best way to call/start a Windows Service every x seconds?

查看:81
本文介绍了每x秒调用/启动Windows服务的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务,该服务基本上是在访问邮箱并阅读电子邮件.我正在尝试找出每x秒检查一次邮箱的最佳方法.

I have a Windows Service which is basically accessing a mailbox and reading the emails. I am trying to figure out the best way to check the mailbox every x seconds.

一种方法是只调用Thread.Sleep,然后再次调用Start方法,例如

One method is to just call Thread.Sleep, then call the Start Method again, e.g.

protected override void OnStart(string[] args)
{
    // get config settings
    CheckMailbox();
}

public void CheckMailbox()
{
    int x = 5000;

    // do stuff

    Thread.Sleep(x);
    CheckMailbox();
}

不确定这是否是最好的解决方法.为了进一步探讨这一点,我了解您可以通过WCF公开服务来调用Windows服务.在这种情况下,如果一个名为Process的Web应用程序启动,那么是否存在冲突的线程,我是否正确?那我该如何处理呢?我是否每次都要创建一个新线程并将其放入队列?

Not sure if this is the best way to go about it. To then further explore this, I understand that you can call a Windows Service by exposing the Service via WCF. In this case, if a web application called the Process to start, there would be conflicting Threads am I correct? How would I then deal with that? Would I have to create a new thread each time and put it in a queue?

推荐答案

睡眠通常不是一个好主意.除了其他方面,这意味着您不会快速响应Stop事件.

Sleeping is usually a bad idea. Apart from anything else, it means that you won't respond to the Stop event quickly.

您应该使用 Timer .有两种适用于服务的内容:

You should use a Timer. There are two which are appropriate for services:

在每种情况下,您基本上都会在每次定时器滴答"以及何时触发时说出要发生的事情.

In each case you basically say what you want to happen each time the timer "ticks" and when you want it to fire.

我的线程教程的部分介绍了可用计时器之间的一些差异.

我不确定您的WCF问题,但是我认为您的Web服务不会明确启动该服务-它只是通过WCF与之联系.是的,您可能需要特别注意线程.确切地说,您需要注意的将取决于WCF服务所公开的内容.

I'm not sure about your WCF question, but I don't think your web service would be explicitly starting the service - it would just be contacting it via WCF. Yes, you'd potentially need to be careful about threads. Exactly what you'll need to be careful of will depend on what the WCF service is exposing.

这篇关于每x秒调用/启动Windows服务的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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