Last active
March 23, 2016 03:20
-
-
Save marcussacana/cd0d47775b8ae7e9a33c to your computer and use it in GitHub Desktop.
Run a Process and dected if he crash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Management; //need add the reference to your project... | |
using System.Windows.Forms; //need add the reference to your project... | |
public class Program { | |
public void Main(string[] args) { | |
if (args.Length != 0) | |
if (args[0] == "crash") { | |
PerformOverflow(); | |
return; | |
} | |
Console.WriteLine(TryRun(Application.ExecutablePath, "crash", AppDomain.CurrentDomain.BaseDirectory) ? "Not Crash" : "Crashed"); | |
} | |
static void PerformOverflow() { | |
PerformOverflow(); | |
} | |
private static bool TryRun(string executablePath, string arg, string RunAt) { | |
System.Diagnostics.Process proc = new System.Diagnostics.Process(); | |
proc.StartInfo = new System.Diagnostics.ProcessStartInfo() { | |
FileName = executablePath, | |
Arguments = arg, | |
UseShellExecute = false, | |
WorkingDirectory = RunAt | |
}; | |
proc.Start(); | |
proc.Refresh(); | |
bool resume = true; | |
bool running = true; | |
while (running && resume) { | |
System.Threading.Thread.Sleep(200); //to low cpu usage | |
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("WerFault"); | |
if (procs.Length != 0){ | |
foreach (System.Diagnostics.Process crash in procs) { | |
string cmd = GetArgs(crash.Id); | |
if (cmd.Contains("-u") && cmd.Contains("-p " + proc.Id)) { | |
resume = false; | |
crash.Kill(); | |
break; | |
} | |
} | |
} | |
try { | |
System.Diagnostics.Process.GetProcessById(proc.Id); | |
} | |
catch { | |
running = false; | |
} | |
} | |
if (running) | |
proc.Kill(); | |
return resume; | |
} | |
static string GetArgs(int PID) { | |
string wmiQuery = string.Format("select CommandLine from Win32_Process where ProcessID='{0}'", PID); | |
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery); | |
ManagementObjectCollection results = searcher.Get(); | |
foreach (ManagementObject result in results) | |
return (string)result["CommandLine"]; | |
throw new Exception("Process Not Found"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment