我正在尝试使用topshelf创建Windows服务。我设法成功创建了我想要的东西并对其进行调试。我也设法使用了安装服务
myservice.exe install
但是尝试使用服务管理器启动服务时,我不断收到以下错误
我参考了以下文章,并尝试通过添加虚拟方法从新线程运行计时器,但这也会产生相同的问题。
Topshelf window service giving Error 1053 when try to start the service
https://github.com/Topshelf/Topshelf/issues/183
我的代码:
class Program
{
static void Main(string[] args)
{
var rc = HostFactory.Run(x => //1
{
// x.StartManually();
x.Service<testclass>(s => //2
{
x.StartManually();
s.ConstructUsing(name => new testclass()); //3
s.WhenStarted(tc => tc.Start()); //4
s.WhenStopped(tc => tc.Stop()); //5
});
x.RunAsLocalSystem(); //6
x.SetDescription("Sample Topshelf Host"); //7
x.SetDisplayName("Stuff"); //8
x.SetServiceName("Stuff"); //9
}); //10
var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode()); //11
Environment.ExitCode = exitCode;
}
}
}
public class testclass
{
public testclass()
{
_timer = new System.Timers.Timer(1000) { AutoReset = true };
_timer.Elapsed += _timer_Elapsed;
startwatching();
}
void startwatching()
{
watcher.Created += new FileSystemEventHandler(copied);
watcher.EnableRaisingEvents = true;
}
public bool Start()
{
var myThread = new Thread(new ThreadStart(foreverWhile));
myThread.Start();
return true;
}
public void Stop() { _timer.Stop(); }
public void foreverWhile()
{
_timer.Start();
}
}
我从“ startwatching”方法启动文件系统监视程序。 我做错了什么?请指教