1、先引用Topshelf

2、创建一个控制台应用程序

3、创建定时方式

《MessageTab》(你要定时执行的类名)



HostFactory.Run(x =>
{
x.Service<MessageTab>(s =>
{
s.ConstructUsing(name => new MessageTab());
s.WhenStarted(tc => tc.OnStart());
s.WhenStopped(tc => tc.OnStop());
});
x.RunAsLocalSystem();

x.SetDescription("ServerRemark"); 服务的描述
x.SetDisplayName("ShowRemark"); //显示名称
x.SetServiceName("ServerName"); //服务名称
});


 

4、写你的逻辑



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

namespace LTS_EcryptPasswordString
{
public class MessageTab
{
readonly Timer _timer;
public MessageTab()
{
_timer = new Timer(1000) { AutoReset = true };
_timer.Elapsed += (sender, eventArgs) => DoWorking();
}
public void OnStart()
{
_timer.Start();
}
public void OnStop()
{
if (_timer.Enabled)
_timer.Stop();
}
void DoWorking()
{
Console.WriteLine("我是你");
Console.ReadKey();
}
}
}