简体   繁体   English

没有OnStop()的服务停止

[英]Service stops without OnStop()

I have a service which creates a FileSystemWatcher inside OnStart and I want the service to end only after a file is created 我有一个在OnStart内创建FileSystemWatcher的服务,我希望该服务仅在创建文件后结束

So I have this code 所以我有这段代码

protected void OnStart()
        {

            try
            {
                using (FileSystemWatcher watcher = new FileSystemWatcher())
                {
                    watcher.Path = WatchPath;
                    watcher.NotifyFilter = NotifyFilters.LastWrite;
                    watcher.Filter = "*.txt";
                    watcher.IncludeSubdirectories = false;
                    watcher.Changed += OnCreated;
                    watcher.EnableRaisingEvents = true;
                    logEvents.Write(MyLogClass.LogLevel.Info, "Watcher created");
                }
            }
            catch (Exception ex)
            {
                logEvents.Write(MyLogClass.LogLevel.Info, ex.Message);
            }

            if (finished)
            {
                OnStop();
            }
        }

The bool "finished" is only set to true in the last line of OnCreated 布尔值“完成”仅在OnCreated的最后一行设置为true

private void OnCreated(object sender, FileSystemEventArgs e)
        {
            //Here I do all the stuff I need and then:
            finished = true;
        }

But even when OnCreated is not executed the service ends and I get: 但是,即使不执行OnCreated,服务也会结束,并且我得到:

"The program - has exited with code 0 (0x0)." “程序-已退出,代码为0(0x0)。”

I've tried calling OnStop() in different parts of the code but the same thing happens anyways. 我试过在代码的不同部分调用OnStop(),但是无论如何都会发生相同的事情。 I'd like the service to stay still until a given file is created and to start over when OnCreated is done, but I don't know what's wrong in this. 我希望该服务在创建给定文件之前保持静止,并在完成OnCreated之后重新开始,但是我不知道这有什么问题。

A FileSystemWatcher , by itself, isn't enough to keep a process running. FileSystemWatcher不足以保持进程运行。 When no events are being serviced, no threads are required, and even when one is required it'll be a background thread. 当没有事件被服务时,不需要线程,即使需要事件,它也将是后台线程。

What a process needs to stay running is a foreground thread . 进程需要保持运行是前台线程 It doesn't matter what that thread is doing, so long as at least one exists. 只要至少存在一个线程,线程在做什么就无关紧要。

It doesn't look like you have any useful work for such a thread to do - but you need one. 看起来您没有任何有用的工作要做这样的线程-但您需要一个。 I suggest you create a thread that just waits for a ManualResetEvent to become set. 我建议您创建一个仅等待ManualResetEvent设置的线程。 You then use that, rather than your finished variable, to indicate when you want the service to shut down. 然后,您可以使用该值(而不是finished变量)来指示何时要关闭服务。

At that point, the foreground thread exits, no foreground threads are left, the process shuts down and the service becomes stopped. 那时,前台线程退出,没有前台线程离开,进程关闭,服务停止。


Note that you have to create a new thread for this waiting work. 请注意,您必须为此等待的工作创建一个新线程。 The thread on which OnStart (and OnStop ) is called doesn't "belong" to you. 调用OnStart (和OnStop )的线程并不属于您。 It's a thread that services the Service Control Manager, and you need to let it do that. 它是为服务控制管理器提供服务的线程,您需要让它执行此操作。

Your code is executing once and then ending. 您的代码执行一次,然后结束。

You need a loop with a sleep timer around the main code to keep it alive. 您需要在主代码周围循环一个带有睡眠计时器的循环,以使其保持活动状态。 And in here embed your check for finished. 并在其中嵌入完成的支票。

Edit: As mentioned in the comments. 编辑:如评论中所述。 I missed that you were doing this in OnStart. 我错过了您在OnStart中执行此操作。 The loop shouldn't be done here otherwise windows will think that you service failed to start. 不应在此处执行循环,否则Windows会认为您的服务无法启动。 So you will also need to refactor this. 因此,您还需要重构它。

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

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