让你的GUI程序随WINDOWS服务一起启动
2011-07-23 14:20
405 查看
本文内容
一.应用场景
二.本文难点
三.关键代码
四.创建Windows服务
五.修改参数并启动服务
源码下载
![](http://hi.csdn.net/attachment/201107/23/0_1311402181Ozdd.gif)
一.应用场景
客户服务器上装的软件越来越多,由原来一个人管理改为几个人同时管理了,因此不同的管理员需要经常进行远程登陆,注销操作。
服务器上原有的一些程序是windows程序,用户登陆注销后,程序就退出了,咋办?把GUI程序改成windows服务程序是可行的,但客户希望能尽快解决此问题啊,咋办?于是就有了本文的想法,先这样用用吧! 呵呵...
二.本文难点
说起来就一句话,做起来可得考虑以下三个问题:
1.如程序要访问Oracle数据库,在启动你的程序前,得先将Oracle数据库服务启动。
2.让Windows服务程序支持启动你的有界面的应用程序。(默认是不能与桌面交互的)
3.如何让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.3 检查并启动对应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.4 为当前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());
}
}
执行效果: (不再需要手动去设置)
一.应用场景
二.本文难点
三.关键代码
四.创建Windows服务
五.修改参数并启动服务
源码下载
![](http://hi.csdn.net/attachment/201107/23/0_1311402181Ozdd.gif)
一.应用场景
客户服务器上装的软件越来越多,由原来一个人管理改为几个人同时管理了,因此不同的管理员需要经常进行远程登陆,注销操作。
服务器上原有的一些程序是windows程序,用户登陆注销后,程序就退出了,咋办?把GUI程序改成windows服务程序是可行的,但客户希望能尽快解决此问题啊,咋办?于是就有了本文的想法,先这样用用吧! 呵呵...
二.本文难点
说起来就一句话,做起来可得考虑以下三个问题:
1.如程序要访问Oracle数据库,在启动你的程序前,得先将Oracle数据库服务启动。
2.让Windows服务程序支持启动你的有界面的应用程序。(默认是不能与桌面交互的)
3.如何让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.3 检查并启动对应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.4 为当前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());
}
}
执行效果: (不再需要手动去设置)
![](http://hi.csdn.net/attachment/201107/23/0_1311401979Mu2Q.gif)
相关文章推荐
- 让你的GUI程序随WINDOWS服务一起启动
- exe程序作为WINDOWS服务显示界面启动方法
- 想让程序开机启动有管理员权限,使用 Windows 服务吧!
- windows服务下启动外部程序
- 将windows程序 注册成为系统服务自动启动
- windows 10安装mysql 5.7后,启动服务提示“本地计算机上的mysql服务启动后停止,某些服务在未由其他服务或程序使用时将自动停止。
- windows start 命令启动 GUI 程序
- Win2000/XP安装软件时提示“无法访问windows安装程序,服务中windows Installer状态为停止,不能启动”
- 【原创】把 Nginx 创建为 Windows 的一个服务,关联PHP服务一起启动
- 让你写的c#程序一开机就自动运行 Windows服务实现开机启动
- 将linux下c程序移植到windows并作为服务启动
- 基于visual c++之windows核心编程代码分析(45)利用Windows系统服务启动程序
- C# 自己写了一个windows服务程序,运行时错误: 错误 1053: 服务没有及时响应启动或控制请求。(推荐)
- windows中系统开机自动运行程序和自动启动服务
- 基于visual c++之windows核心编程代码分析(45)利用Windows系统服务启动程序
- 如何在Windows服务中以当前用户启动一个程序
- C#实现WinForm随WINDOWS服务一起启动(转载)
- Python 编写Windows服务程序:将Python作为Windows服务启动
- Windows Service开发日志四(用程序设置服务的运行状态及启动方式)
- windows中系统开机自动运行程序和自动启动服务