【发布时间】:2012-08-30 07:39:55
【问题描述】:
如果我的服务正在启动或停止,我会让这段代码运行一个 powershell 脚本。
Timer timer1 = new Timer();
ServiceController sc = new ServiceController("MyService");
protected override void OnStart(string[] args)
{
timer1.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer1.Interval = 10000;
timer1.Enabled = true;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
if ((sc.Status == ServiceControllerStatus.StartPending) || (sc.Status == ServiceControllerStatus.Stopped))
{
StartPs();
}
}
private void StartPs()
{
PSCommand cmd = new PSCommand();
cmd.AddScript(@"C:\windows\security\dard\StSvc.ps1");
PowerShell posh = PowerShell.Create();
posh.Commands = cmd;
posh.Invoke();
}
当我从 cmd 提示符终止我的服务时,它工作正常 但即使我的服务已启动并正在运行,powershell 脚本也会继续自行执行(它会在计算机上附加一个文件) 知道为什么吗?
【问题讨论】:
-
说powershell与这个问题正交是否正确,而真正的问题是:为什么我的
StartPending/Stopped检查不能正常工作? -
你试过设置断点看看到底发生了什么吗?
标签: c# windows-services