如何为 Windows 编写空白服务二进制文件?
How to write a blank service binary for Windows?
我正在尝试创建一个完全不用于测试目的的服务。为此,我需要一个绝对什么都不做的二进制文件,但服务似乎不会为任何可执行文件启动,只有那些专门设计为服务二进制文件的文件。我试图找到有关如何制作服务二进制文件的信息,但似乎找不到任何信息。提前致谢。
查看这个小 nuget 库中的演示服务:
https://github.com/wolfen351/windows-service-gui
它应该为您提供一个使用测试服务的良好起点,它是一个什么都不做的 windows 服务的简单实现。还有nuget包会帮你运行的! :)
这是代码的核心:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
namespace DemoService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
Timer t1 = new Timer(AutoStopCallback, null, 15000, -1); // auto stop in 15 seconds for testing
}
private void AutoStopCallback(object state)
{
Stop();
}
protected override void OnStart(string[] args)
{
Thread.Sleep(2000);
base.OnStart(args);
}
protected override void OnStop()
{
Thread.Sleep(2000);
base.OnStop();
}
protected override void OnContinue()
{
Thread.Sleep(2000);
}
protected override void OnPause()
{
Thread.Sleep(2000);
}
}
}
我正在尝试创建一个完全不用于测试目的的服务。为此,我需要一个绝对什么都不做的二进制文件,但服务似乎不会为任何可执行文件启动,只有那些专门设计为服务二进制文件的文件。我试图找到有关如何制作服务二进制文件的信息,但似乎找不到任何信息。提前致谢。
查看这个小 nuget 库中的演示服务:
https://github.com/wolfen351/windows-service-gui
它应该为您提供一个使用测试服务的良好起点,它是一个什么都不做的 windows 服务的简单实现。还有nuget包会帮你运行的! :)
这是代码的核心:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
namespace DemoService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
Timer t1 = new Timer(AutoStopCallback, null, 15000, -1); // auto stop in 15 seconds for testing
}
private void AutoStopCallback(object state)
{
Stop();
}
protected override void OnStart(string[] args)
{
Thread.Sleep(2000);
base.OnStart(args);
}
protected override void OnStop()
{
Thread.Sleep(2000);
base.OnStop();
}
protected override void OnContinue()
{
Thread.Sleep(2000);
}
protected override void OnPause()
{
Thread.Sleep(2000);
}
}
}