awk常用的几个统计命令
2016-04-06 14:42
1226 查看
功能:
每三十分钟扫描进程,如果不存在进程fromdemo.exe.则启动该应用程序.
1.检测进程进否存在
代码
///
<summary>
///检查进程是否已启动
///
</summary>
///
<param name="processName"></param>
///
<returns></returns>
private static bool IsExistProcess(string processName)
{
Process[] MyProcesses = Process.GetProcessesByName(processName);
if (MyProcesses != null && MyProcesses.Length > 0)
{
return true;
}
return false;
}
2.建一个windows services 工程,添加一个System.Timers.Timer来使用.
// Create a timer with a 60*5 second interval.
aTimer = new System.Timers.Timer(60000*30);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled = true;
在OnTimedEvent里写要处理的内容.
代码if(!IsExistProcess("FormDemo"))
{
Process p=
newProcess();
stringlocation=System.Reflection.Assembly.GetExecutingAssembly().Location;
stringdir=location.Substring(0, location.LastIndexOf("\\")+
1);
p.StartInfo.FileName=dir+
"FormDemo.exe";
p.Start();
}
}
3.添加安装程序.我使用localsystem使用户,自动启动
在ServiceProcessInstaller的onCommited事件里设置服务可以与桌面交互,否则会看不到界面,虽然进程里面会有.
代码
//允许桌面交互
private
voidserviceProcessInstaller_Committed(objectsender, InstallEventArgs e)
{
try
{
ConnectionOptions myConOptions=
newConnectionOptions();
myConOptions.Impersonation=ImpersonationLevel.Impersonate;
ManagementScope mgmtScope=
newSystem.Management.ManagementScope(@"root\CIMV2", myConOptions);
mgmtScope.Connect();
ManagementObject wmiService=
newManagementObject("Win32_Service.Name='"
+serviceInstaller.ServiceName+
"'");
ManagementBaseObject InParam=wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"]=
true;
ManagementBaseObject OutParam=wmiService.InvokeMethod("Change", InParam,null);
#regionWindows服务安装后自动启动
Process p=
newProcess();
p.StartInfo.FileName=
"cmd.exe";
p.StartInfo.UseShellExecute=
false;
p.StartInfo.RedirectStandardInput=
true;
p.StartInfo.RedirectStandardOutput=
true;
p.StartInfo.RedirectStandardError=
true;
p.StartInfo.CreateNoWindow=
true;
p.Start();
stringCmdstring=
"net start"
+serviceInstaller.ServiceName;
p.StandardInput.WriteLine(Cmdstring);
p.StandardInput.WriteLine("exit");
#endregion
}
catch
{
EventLog.WriteEvent("test failer",null,null,null);
}
}
第一次我使用安装项目进行安装,没有顺利进行,后来改为批处理了,直接把对应版本的installutil.exe拷由到服务所在路径,然后
installutil /i myservice.exe
net start myservice
pause
每三十分钟扫描进程,如果不存在进程fromdemo.exe.则启动该应用程序.
1.检测进程进否存在
代码
///
<summary>
///检查进程是否已启动
///
</summary>
///
<param name="processName"></param>
///
<returns></returns>
private static bool IsExistProcess(string processName)
{
Process[] MyProcesses = Process.GetProcessesByName(processName);
if (MyProcesses != null && MyProcesses.Length > 0)
{
return true;
}
return false;
}
2.建一个windows services 工程,添加一个System.Timers.Timer来使用.
// Create a timer with a 60*5 second interval.
aTimer = new System.Timers.Timer(60000*30);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled = true;
在OnTimedEvent里写要处理的内容.
代码if(!IsExistProcess("FormDemo"))
{
Process p=
newProcess();
stringlocation=System.Reflection.Assembly.GetExecutingAssembly().Location;
stringdir=location.Substring(0, location.LastIndexOf("\\")+
1);
p.StartInfo.FileName=dir+
"FormDemo.exe";
p.Start();
}
}
3.添加安装程序.我使用localsystem使用户,自动启动
在ServiceProcessInstaller的onCommited事件里设置服务可以与桌面交互,否则会看不到界面,虽然进程里面会有.
代码
//允许桌面交互
private
voidserviceProcessInstaller_Committed(objectsender, InstallEventArgs e)
{
try
{
ConnectionOptions myConOptions=
newConnectionOptions();
myConOptions.Impersonation=ImpersonationLevel.Impersonate;
ManagementScope mgmtScope=
newSystem.Management.ManagementScope(@"root\CIMV2", myConOptions);
mgmtScope.Connect();
ManagementObject wmiService=
newManagementObject("Win32_Service.Name='"
+serviceInstaller.ServiceName+
"'");
ManagementBaseObject InParam=wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"]=
true;
ManagementBaseObject OutParam=wmiService.InvokeMethod("Change", InParam,null);
#regionWindows服务安装后自动启动
Process p=
newProcess();
p.StartInfo.FileName=
"cmd.exe";
p.StartInfo.UseShellExecute=
false;
p.StartInfo.RedirectStandardInput=
true;
p.StartInfo.RedirectStandardOutput=
true;
p.StartInfo.RedirectStandardError=
true;
p.StartInfo.CreateNoWindow=
true;
p.Start();
stringCmdstring=
"net start"
+serviceInstaller.ServiceName;
p.StandardInput.WriteLine(Cmdstring);
p.StandardInput.WriteLine("exit");
#endregion
}
catch
{
EventLog.WriteEvent("test failer",null,null,null);
}
}
第一次我使用安装项目进行安装,没有顺利进行,后来改为批处理了,直接把对应版本的installutil.exe拷由到服务所在路径,然后
installutil /i myservice.exe
net start myservice
pause
相关文章推荐
- C#进行SQL执行Insert插入语句后返回新增行的ID
- java 中 mongodb的各种操作(二)
- 机器学习中距离和相似性度量方法
- 《SPRING实战第三版》--源自技术
- C#如何防止程序多次运行的技巧
- java实现线程的两种方式
- gcc编译参数-fPIC的一些问题
- java中的多线程包--Semaphore 实现 互斥 与 连接池
- ios Block详细用法
- ansII
- Peeking into Apache Flink's Engine Room
- Python Twisted系列教程16:Twisted 进程守护
- VFL(Visual Format Language)语言
- cookie的获取,建立和删除(放服务器上才能起效)
- 51nod 1344 走格子
- PowerShell 多线程的使用
- HDU1166 敌兵布阵 线段树 点修改 求和
- Windows下Java+MyBatis框架+MySQL的开发环境搭建教程
- 欢迎使用CSDN-markdown编辑器
- javascript:void(0)和javascript:;的用法