好吧,所以我正在创建一个Windows服务,它将根据用户是否登录来启动和停止我的渲染可执行文件。我已经确定我想从服务运行exe,并且知道它将在没有gui的安全桌面上运行。这对我来说不是问题。
问题在于,当我告诉它开始时,我的服务会立即启动。我认为这意味着我错过了某种循环或事件处理程序或SOMETHING,但我不知道是什么。这是我尝试编程的第一个服务。由于似乎没有办法有效地调试服务,我希望能从这里的聪明人中获得一些意见。
非常感谢任何输入!这是代码:
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.Management;
namespace SessionService
{
public partial class renderServ : ServiceBase
{
private readonly ManagementClass _wmiComputerSystem = new ManagementClass("Win32_ComputerSystem");
private Process process;
Boolean isRunning;
public renderServ()
{
InitializeComponent();
process = new Process();
process.StartInfo = new ProcessStartInfo(@"C:\Windows\notepad.exe");
isRunning = false;
}
public void OnUserLogin()
{
/*if (isRunning == true)
{
process.Kill();
process.WaitForExit();
}*/
isRunning = false;
}
public void OnUserLogoff()
{
if (isRunning == false)
{
process.Start();
process.PriorityClass = ProcessPriorityClass.Idle;
}
isRunning = true;
}
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
base.OnSessionChange(changeDescription);
switch (changeDescription.Reason)
{
case SessionChangeReason.SessionLogon:
{
OnUserLogin();
} break;
case SessionChangeReason.SessionLogoff:
{
OnUserLogoff();
} break;
case SessionChangeReason.SessionLock:
{
OnUserLogoff();
} break;
case SessionChangeReason.SessionUnlock:
{
OnUserLogin();
} break;
}
}
protected override void OnStart(string[] args)
{
CanHandleSessionChangeEvent = true;
System.Windows.Forms.Application.Run();
}
protected override void OnStop()
{
}
}
}