让Topshelf服务在启动时运行一次,然后停止

tec*_*ble 3 windows-services topshelf

我已经写了一个TopShelf服务/控制台应用程序,它似乎按预期运行,除了我希望它在启动时运行一次,然后禁用自己直到下次启动/重启.

我希望这会奏效:

class MyServiceClass
{
    public void Start()
    {
        // do the things that need doing

        this.Stop();
    }

    public void Stop()
    {
    }
Run Code Online (Sandbox Code Playgroud)

但这不起作用,大概是因为this.Stop()命令用于清理,而不是导致服务停止.

我的Program.cs看起来像这样:

// Uses Topshelf: http://topshelf-project.com/
// Guided by: http://www.ordina.nl/nl-nl/blogs/2013/maart/building-windows-services-with-c-and-topshelf/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Topshelf;

namespace MyNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            HostFactory.Run(hostConfigurator =>
            {
                hostConfigurator.Service<MyServiceClass>(serviceConfigurator =>
                {
                    serviceConfigurator.ConstructUsing(() => new MyServiceClass());
                    serviceConfigurator.WhenStarted(myServiceClass => myServiceClass.Start());
                    serviceConfigurator.WhenStopped(myServiceClass => myServiceClass.Stop());
                });

                hostConfigurator.RunAsLocalSystem();

                hostConfigurator.SetDisplayName("MyService");
                hostConfigurator.SetDescription("Does stuff.");
                hostConfigurator.SetServiceName("MyService");
                hostConfigurator.StartAutomatically();
                hostConfigurator.EnableShutdown();
            });
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

如何在执行结束时停止服务?

更新:根据达米恩的意见,我现在有:

public class MyServiceClass
{
    private readonly Task task;
    private HostControl hostControl;

    public MyServiceClass()
    {
        task = new Task(DoWork);
    }

    private void DoWork()
    {
        Console.WriteLine("Listen very carefully, I shall say this only once");
        hostControl.Stop();
    }

    public void Start(HostControl hostControl)
    {
        // so we can stop the service at the end of the check
        this.hostControl = hostControl;

        // start the DoWork thread
        task.Start();
    }

    public void Stop()
    {

    }

};
Run Code Online (Sandbox Code Playgroud)

和更新的Program.cs

class Program
{
    static void Main(string[] args)
    {

        HostFactory.Run(hostConfigurator =>
        {
            hostConfigurator.Service<MyServiceClass>((serviceConfigurator =>
            {
                serviceConfigurator.ConstructUsing(() => new MyServiceClass());
                serviceConfigurator.WhenStarted((myServiceClass, hostControl) => myServiceClass.Start(hostControl));
                serviceConfigurator.WhenStopped(myServiceClass => myServiceClass.Stop());
            });    /* compiler thinks there's a ")" missing from this line */

            hostConfigurator.RunAsLocalSystem();

            hostConfigurator.SetDisplayName("MyService");
            hostConfigurator.SetDescription("Does stuff.");
            hostConfigurator.SetServiceName("MyService");
            hostConfigurator.StartAutomatically();
            hostConfigurator.EnableShutdown();
        });

    }
};
Run Code Online (Sandbox Code Playgroud)

但是,这不会编译.我的编译器建议在我的注释(或上面的代码中显示)上缺少")",但是任何添加的右括号都会添加到错误列表中.

我觉得我很接近......任何想法?

tec*_*ble 6

最终得到了这个工作:

public class MyServiceClass : ServiceControl
{
    private readonly Task task;
    private HostControl hostControl;

    public MyServiceClass()
    {
        task = new Task(DoWork);
    }

    private void DoWork()
    {
        Console.WriteLine("Listen very carefully, I shall say this only once");
        //hostControl.Stop();
    }

    public bool Start(HostControl hostControl)
    {
        // so we can stop the service at the end of the check
        this.hostControl = hostControl;

        // start the DoWork thread
        task.Start();

        return true;
    }

    public bool Stop(HostControl hostControl)
    {
        return true;
    }

};
Run Code Online (Sandbox Code Playgroud)

class Program
{
    static void Main(string[] args)
    {

        HostFactory.Run(hostConfigurator =>
        {
            hostConfigurator.Service<MyServiceClass>(serviceConfigurator =>
            {
                serviceConfigurator.ConstructUsing(() => new MyServiceClass());
                serviceConfigurator.WhenStarted((myServiceClass, hostControl) => myServiceClass.Start(hostControl));
                serviceConfigurator.WhenStopped((myServiceClass, hostControl) => myServiceClass.Stop(hostControl));
            });

            hostConfigurator.RunAsLocalSystem();

            hostConfigurator.SetDisplayName("MyService");
            hostConfigurator.SetDescription("Does stuff.");
            hostConfigurator.SetServiceName("MyService");
            hostConfigurator.StartAutomatically();
            hostConfigurator.EnableShutdown();
        });

    }
};
Run Code Online (Sandbox Code Playgroud)

我的第二次尝试有一个备用的"("在第一次提到serviceConfigurator时,我需要将我的void Start和Stop函数转换为bool函数.希望这有助于某人.