Windows Service in c#
-
hi, I am making Windows Service(User defined) I have written code inside OnStart within Windows Service Inside the onstart i want that suppose a.exe is already running I want to close that a.exe and start a.exe from windows Service When i run the windows Service two exe runs cocuurently. On Start i have written Process.Start(@"c:\a.exe"); I want to identify in beginning of any process of a.exe if exist how can i check in windows service OnStart() protected override void OnStart(string[] args) { // TODO: Add code here to start your service. Process[] aProcesses1; if (aProcesses1.Length > 0) { if(rd1==true) { // The last excel process is the orphaned process which is killed aProcesses1[aProcesses1.Length-1].Kill(); rd1=false; } } int intExcelPID; aProcesses1=Process.GetProcessesByName("a"); string strProc="These are the processes running \n"; for (int i = 0; i <= aProcesses1.GetUpperBound(0); i++) { intExcelPID = aProcesses1[i].Id; strProc+=intExcelPID.ToString()+ " "; } //Retrive from registry the value of Path RegistryKey hklm =Registry.LocalMachine; hklm=hklm.OpenSubKey("SOFTWARE\\man"); Object obp=hklm.GetValue("Path"); string file=obp.ToString() + "\\man.exe"; Process.Start(@file); } Thanks sanjeev
-
hi, I am making Windows Service(User defined) I have written code inside OnStart within Windows Service Inside the onstart i want that suppose a.exe is already running I want to close that a.exe and start a.exe from windows Service When i run the windows Service two exe runs cocuurently. On Start i have written Process.Start(@"c:\a.exe"); I want to identify in beginning of any process of a.exe if exist how can i check in windows service OnStart() protected override void OnStart(string[] args) { // TODO: Add code here to start your service. Process[] aProcesses1; if (aProcesses1.Length > 0) { if(rd1==true) { // The last excel process is the orphaned process which is killed aProcesses1[aProcesses1.Length-1].Kill(); rd1=false; } } int intExcelPID; aProcesses1=Process.GetProcessesByName("a"); string strProc="These are the processes running \n"; for (int i = 0; i <= aProcesses1.GetUpperBound(0); i++) { intExcelPID = aProcesses1[i].Id; strProc+=intExcelPID.ToString()+ " "; } //Retrive from registry the value of Path RegistryKey hklm =Registry.LocalMachine; hklm=hklm.OpenSubKey("SOFTWARE\\man"); Object obp=hklm.GetValue("Path"); string file=obp.ToString() + "\\man.exe"; Process.Start(@file); } Thanks sanjeev
Going back to your original post, I already pointed out HUGE problems with your code, that by looking at this post, you haven't fixed at all. But, to find all the "a" processes and kill them, then restart a new one:
Process[] foundProcs = Process.GetProcessesByName(@"a");
foreach (Process proc in foundProcs)
{
proc.Kill();
}
Process.Start(@"notepad");RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome