Windows服务只能部分工作
本文关键字:工作 能部 服务 Windows | 更新日期: 2023-09-27 17:59:24
我第一次创建了Windows服务。它不起作用。创建了文件OnStart.txt和OnStop.txt,但没有发生其他任何事情。我不相信MSProcess会运行。如何进行故障排除?
namespace MailScan_Service
{
public partial class svMaster : ServiceBase
{
private Timer myTimer = new Timer();
public svMaster()
{
InitializeComponent();
}
ServiceController sc = new ServiceController("MailScan Service");
protected override void OnStart(string[] args)
{
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt");
myTimer.Start();
}
protected override void OnStop()
{
if (myTimer.Enabled == true)
myTimer.Stop();
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStop.txt");
}
private void myTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (!sc.Status.Equals(ServiceControllerStatus.StopPending))
{
MSSettings msSet = new MSSettings();
msSet.Load();
myTimer.Interval = msSet.ScanTimer * 60000;
MSProcess.Start();
msSet.Dispose();
}
}
}
然而,这个模拟器工作得很好!
namespace MailScanSettings
{
public partial class FormSimService : Form
{
public FormSimService()
{
InitializeComponent();
}
private void btnGo_Click(object sender, EventArgs e)
{
tbStatus.Clear();
tbStatus.Text += "Running";
this.BackColor = Color.Green;
timer1.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
timer1.Stop();
this.BackColor = Color.Red;
tbStatus.Clear();
tbStatus.Text += "Stopped";
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
MSSettings msSet = new MSSettings();
msSet.Load();
timer1.Interval = msSet.ScanTimer * 60000;
MSProcess.Start();
msSet.Dispose();
}
}
}
我过去必须调试很多Windows服务,并采用了相同的日志文件pot-shot方法。这无疑是痛苦和沮丧的。
最终,我在某个地方找到了一个提示,即出于调试目的,您可以放弃这一行:
System.Diagnostics.Debugger.Launch()
在OnStart方法的开头。它将弹出一个对话框,询问您要附加哪个调试器。由于您已经在Visual Studio中加载了解决方案,所以只需选择它,它就会到达您的断点。
它也可能很乏味,但更有效率。你最终会陷入一个生产力循环,不得不停止服务、卸载、修补代码、构建、重新安装、启动、调试。。。