C#之TopShelf启动Windows服务 原文链接:https://blog.csdn.net/qq_36664495/java/article/details/90600995

 

1、项目的主要运行代码

HostFactory.Run(x =>
{
x.RunAsLocalSystem();
x.SetDescription("topshelf测试");
x.SetDisplayName("topshelftest");
x.SetServiceName("topshelftest");

x.Service<TopshelfTest>(s =>
{
s.ConstructUsing(name => new TopshelfTest());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
});
using System;
using System.Timers;

namespace TopshelfDemo
{
public class TopshelfTest
{
readonly Timer timer;
public TopshelfTest()
{
timer = new Timer(1000) { AutoReset = true };
timer.Elapsed += (sender, eventArgs) => { Run(); };
}
public void Start() { timer.Start(); }
public void Stop() { timer.Stop(); }
public static void Run()
{
Console.WriteLine("hello TopShelf");
}
}
}
2、服务安装、启动和卸载

使用管理员进入cmd

进入相应的文件夹位置

TopshelfDemo.exe install 注册

TopshelfDemo.exe start启动

TopshelfDemo.exe uninstall 卸载

 

 

win+R,输入services.msc查看


————————————————
版权声明:本文为CSDN博主「望生帝」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36664495/java/article/details/90600995

posted @ 2020-05-09 16:12  2eggs  Views(384)  Comments(0Edit  收藏  举报