[英]Get The Procces ID of a service on OnStart of the service
I have a Windows Service that i create. 我有一个自己创建的Windows服务。
I need to get the PID of the process when running and save it to DB. 我需要在运行时获取进程的PID,并将其保存到DB。
On My OnStart 在我的OnStart上
timer1 = new Timer();
double Interval = Convert.ToDouble(ConfigurationManager.AppSettings["intervalInSeconds"].ToString());
this.timer1.Interval = Interval * 1000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled = true;
Library.WriteErrorLog("service started");
I Tried to get the PID using 我试图使用
GetService but it dosent work. GetService,但可以正常工作。
You can query if from the Managementobject like list 您可以从“管理对象”列表中查询是否
private static uint GetProcessIDByServiceName(string serviceName)
{
uint processId = 0;
string qry = "SELECT PROCESSID FROM WIN32_SERVICE WHERE NAME = '" + serviceName + "'";
System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(qry);
foreach (System.Management.ManagementObject mngntObj in searcher.Get())
{
processId = (uint)mngntObj["PROCESSID"];
}
return processId;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.