Init
This commit is contained in:
109
ManagedProcess.cs
Normal file
109
ManagedProcess.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Configuration;
|
||||
using System.Threading.Tasks;
|
||||
using MarketDataLib.Utility;
|
||||
using MarketData.Utils;
|
||||
using System.Diagnostics;
|
||||
using System.Security;
|
||||
|
||||
namespace Watchdog
|
||||
{
|
||||
|
||||
// This is just a helper app for building and testing stuff for the service
|
||||
|
||||
public class ManagedProcess
|
||||
{
|
||||
public ManagedProcess(string name, string pathToExecutable,bool runAs)
|
||||
{
|
||||
this.Name=name;
|
||||
this.PathToExecutable=pathToExecutable;
|
||||
this.RunAs=runAs;
|
||||
}
|
||||
public String Name{get;set;}
|
||||
public String PathToExecutable{get;set;}
|
||||
public bool RunAs{get;set;}
|
||||
public bool IsRunning()
|
||||
{
|
||||
Process[] pname = Process.GetProcessesByName(Name);
|
||||
if (pname.Length == 0)return false;
|
||||
return true;
|
||||
}
|
||||
public bool Run()
|
||||
{
|
||||
ProcessStartInfo processStartInfo=new ProcessStartInfo();
|
||||
processStartInfo.FileName=PathToExecutable;
|
||||
processStartInfo.WorkingDirectory=Utility.GetPath(processStartInfo.FileName);
|
||||
processStartInfo.UseShellExecute=true;
|
||||
if(RunAs)processStartInfo.Verb="runas";
|
||||
processStartInfo.Domain="";
|
||||
Process process = Process.Start(processStartInfo);
|
||||
if(null == process)return false;
|
||||
if(process.HasExited)return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class ProcessManager
|
||||
{
|
||||
private ProcessManager()
|
||||
{
|
||||
}
|
||||
public static void ManageProcesses(List<ManagedProcess> processList)
|
||||
{
|
||||
foreach(ManagedProcess managedProcess in processList)
|
||||
{
|
||||
if(!managedProcess.IsRunning())
|
||||
{
|
||||
managedProcess.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void ManageProcesses(List<ManagedProcess> processList,EventLog eventLog)
|
||||
{
|
||||
foreach(ManagedProcess managedProcess in processList)
|
||||
{
|
||||
if(!managedProcess.IsRunning())
|
||||
{
|
||||
bool success=managedProcess.Run();
|
||||
eventLog.WriteEntry(String.Format("Process '{0}' was started with {1}",managedProcess.Name,success?"success":"failure"));
|
||||
}
|
||||
else
|
||||
{
|
||||
eventLog.WriteEntry(String.Format("Process '{0}' is running",managedProcess.Name));
|
||||
}
|
||||
}
|
||||
}
|
||||
public static List<ManagedProcess> GetManagedProceses()
|
||||
{
|
||||
List<ManagedProcess> managedProcessList=new List<ManagedProcess>();
|
||||
try
|
||||
{
|
||||
int watchCount=int.Parse(ConfigurationManager.AppSettings["watchcount"]);
|
||||
for(int index=0;index<watchCount;index++)
|
||||
{
|
||||
NVPCollection nvpCollection=new NVPCollection();
|
||||
String key="watch_"+(index+1);
|
||||
String value=ConfigurationManager.AppSettings[key];
|
||||
String[] strItems=value.Split('|');
|
||||
foreach(String strItem in strItems)
|
||||
{
|
||||
NVP nvp=new NVP(strItem);
|
||||
nvpCollection.Add(nvp);
|
||||
}
|
||||
NVPDictionary nvpDictionary=nvpCollection.ToDictionary();
|
||||
ManagedProcess managedProcess=new ManagedProcess(nvpDictionary["Name"].Get<String>(),nvpDictionary["Executable"].Get<String>(),nvpDictionary["RunAs"].Get<bool>());
|
||||
managedProcessList.Add(managedProcess);
|
||||
}
|
||||
return managedProcessList;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
String message=exception.InnerException.Message;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user