如何在Windows服务启动的GUI程序
的有关信息介绍如下:关键代码
3.1 获取系统注销信息
方法:通过与Microsoft.Win32.SessionEndingEventHandler建关关联获取系统注销信息.
protected override void OnStart(string[] args)
{
//获取系统注销,关机
Microsoft.Win32.SystemEvents.SessionEnding += new Microsoft.Win32.SessionEndingEventHandler(this.SystemEvents_SessionEnding);
}
protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
//切记在服务停止时,移除事件.
Microsoft.Win32.SystemEvents.SessionEnding -= new Microsoft.Win32.SessionEndingEventHandler(this.SystemEvents_SessionEnding);
}
3.2 检查并启动Windows服务
/// <summary>
/// 启动已暂停或停止的服务
/// </summary>
private void StartService()
{
try
{
foreach (string serviceName in rwCnfg.GsServiceNames)
{
ServiceController myService = new ServiceController(serviceName);
ServiceControllerStatus status = myService.Status;
switch (status)
{
case ServiceControllerStatus.ContinuePending:
break;
case ServiceControllerStatus.PausePending:
break;
case ServiceControllerStatus.StartPending:
break;
case ServiceControllerStatus.Running:
break;
case ServiceControllerStatus.Paused:
case ServiceControllerStatus.Stopped:
{
myService.Start();
myService.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 2, 0));
Common.wLog("完成启动服务: " + myService.ServiceName + " . " + System.DateTime.Now.ToString());
}
break;
case ServiceControllerStatus.StopPending:
break;
default:
break;
}
}
}
catch (Exception err)
{
Common.wLog(err.ToString());
}
}
3.2 检查并启动对应GUI程序
/// <summary>
/// 启动所有要启动的程序
/// </summary>
private void StartProgram()
{
try
{
foreach (string ProgramPath in rwCnfg.GsProgramPaths)
{
string fileName = "";
//fileName = System.IO.Path.GetFileName(ProgramPath); //文件名
//string ext = System.IO.Path.GetExtension(ProgramPath); //扩展名
fileName = System.IO.Path.GetFileNameWithoutExtension(ProgramPath);// fileName.Replace(ext, "");
if (!IsExistProcess(fileName))
{
ProcessStartInfo startInfo = new ProcessStartInfo(ProgramPath);
startInfo.WindowStyle = ProcessWindowStyle.Normal;
Process.Start(startInfo);
Common.wLog("完成启动程序: " + fileName + ",完整路径:" + ProgramPath + " . " + System.DateTime.Now.ToString());
System.Threading.Thread.Sleep(3 * 1000); //间隔3秒;
}
}
}
catch (Exception err)
{
Common.wLog(err.ToString());
}
}
/// <summary>
/// 检查该进程是否已启动
/// </summary>
/// <param name="processName"></param>
/// <returns></returns>
private bool IsExistProcess(string processName)
{
Process[] MyProcesses = Process.GetProcesses();
foreach (Process MyProcess in MyProcesses)
{
if (MyProcess.ProcessName.CompareTo(processName) == 0)
{
return true;
}
}
return false;
}
3.3 为当前Windows服务设置可与桌面交互选项
为"serviceProcessInstaller1" 的 Committed 事件添加以下操作:
(注意引入 System.Management 命名空间)
private void serviceProcessInstaller1_Committed(object sender, InstallEventArgs e)
{
try
{
ConnectionOptions myConOptions = new ConnectionOptions();
myConOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", myConOptions);
mgmtScope.Connect();
ManagementObject wmiService = new ManagementObject("Win32_Service.Name='" + serviceInstaller1.ServiceName + "'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
}
catch (Exception err)
{
Common.wLog(err.ToString());
}
}
执行效果: (不再需要手动去设置)