我正在使用TopShelf来托管我的Windows服务。这是我的设置代码:强制使用TopShelf服务的单个实例
static void Main(string[] args)
{
var host = HostFactory.New(x =>
{
x.Service<MyService>(s =>
{
s.ConstructUsing(name => new MyService());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
x.RunAsLocalSystem();
x.SetDescription(STR_ServiceDescription);
x.SetDisplayName(STR_ServiceDisplayName);
x.SetServiceName(STR_ServiceName);
});
host.Run();
}
我需要确保只有一个应用程序实例可以同时运行。目前,您可以同时将其作为Windows服务和任意数量的控制台应用程序启动。如果应用程序在启动期间检测到其他实例,它应该退出
我真的很喜欢mutex的方法,但不知道如何使这与TopShelf合作。