Windows服务:OnStart中环 - 我需要授权? [英] Windows Services: OnStart loop - do I need to delegate?

查看:147
本文介绍了Windows服务:OnStart中环 - 我需要授权?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口服务,扫描一个文件夹每n秒的变化。我得到尝试启动它来启动命令的服务没有响应及时的方式。

I've got a windows service which scans a folder every n seconds for changes. I'm getting "the service did not respond to the start command in a timely fashion" when trying to start it up.

我有一个循环中的OnStart掀起像这样:

I've got a loop setting off in OnStart like so:

 public void OnStart(string[] args)
 {
    while (!_shouldExit)
    {
        //Do Stuff

        //Repeat
        Thread.Sleep(_scanIntervalMillis);
    }
 }

这是什么原因造成的错误?我应该委托该方法?

Is this what is causing the error? Should I delegate this method?

推荐答案

的OnStart应该只有开始的工作;这是不负责的这样做。这通常意味着生成一个新的线程来完成实际工作。预计的OnStart及时完成。例如:

OnStart should only start the work; it isn't responsible for doing it. This typically means spawning a new thread to do the actual work. It is expected that OnStart completes promptly. For example:

    public void OnStart(string[] args) // should this be override?
    {
        var worker = new Thread(DoWork);
        worker.Name = "MyWorker";
        worker.IsBackground = false;
        worker.Start();
    }
    void DoWork()
    {
        // do long-running stuff
    }

这篇关于Windows服务:OnStart中环 - 我需要授权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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