zoukankan      html  css  js  c++  java
  • Topshelf 操作服务

    Topshelf 是第三方框架,需要从Nuget包中下载安装

    static void Main(string[] args)
    {
      Host host = HostFactory.New(x =>
      {
        // 基本的配置
        x.RunAsLocalSystem();
        x.SetServiceName("TestService");
        x.SetDisplayName("TestService");
        x.SetDescription("测试服务");
        x.StartAutomatically();
        x.EnableShutdown();

      // 注册服务
      x.Service<TestService>(h => new TestService());

      // 设置服务失败后的操作,分别对应第一次、第二次、后续
      x.EnableServiceRecovery(t =>
      {
        t.RestartService(0);     
        t.RestartService(0);
        t.RestartService(0);
        t.OnCrashOnly();
      });
    });

      host.Run();
    }

    // 增加类:

    public class TestService : ServiceControl
    {
      public bool Start(HostControl hostControl)
      {
        //开始

    FileStream fs = new FileStream("E:\TestService.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
    StreamWriter sw = new StreamWriter(fs);
    for (int i = 0; i < 10; i++)
    {
      sw.WriteLine("Test-" + i);
    }
    sw.Flush();
    fs.Close();

      return true;
    }

    public bool Stop(HostControl hostControl)
    {
      // 结束

      FileStream fs = new FileStream("E:\TestService.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
      StreamWriter sw = new StreamWriter(fs);
      sw.WriteLine("结束");
      sw.Flush();
      fs.Close();

        return true;
      }
    }

  • 相关阅读:
    第四次寒假作业
    寒假作业三
    寒假作业二
    关于C语言
    寒假作业2代码
    计算机小白
    软工第二次作业
    新开始
    android 自定义滑动按钮
    新知识 HtMl 5
  • 原文地址:https://www.cnblogs.com/wangye520/p/11661271.html
Copyright © 2011-2022 走看看