Skip to content

Instantly share code, notes, and snippets.

@Immerseit
Created June 27, 2011 08:42
Show Gist options
  • Save Immerseit/1048514 to your computer and use it in GitHub Desktop.
Save Immerseit/1048514 to your computer and use it in GitHub Desktop.
Simple Process Start & Kill By Threads
static void Main()
{
//Dictionary if there happen to be different software to run, per row.
Dictionary<string,string> actions = new Dictionary<string,string>();
actions.Add("http://www.site1.com", @"C:\Program Files\Mozilla Firefox\firefox.exe");
actions.Add("http://site2.com", @"C:\Program Files\Mozilla Firefox\firefox.exe");
actions.Add("http://site3.org/", @"C:\Program Files\Mozilla Firefox\firefox.exe");
foreach (var row in actions)
{
var thread = new Thread(
() => {
StartProc(row.Value, row.Key);
});
thread.Start();
}
}
static void StartProc(string name, string param)
{
DateTime runtime = DateTime.MinValue;
int procId = Process.Start(name, param).Id;
Thread.Sleep(1000);
var thread = new Thread(
() =>
KillProc(procId, DateTime.Now)
);
thread.Start();
}
static void KillProc(int procId, DateTime runTime)
{
bool areTimedOut = false;
while (!areTimedOut)
{
if (runTime <= DateTime.Now.AddSeconds(-15))
{
try
{
Process p = Process.GetProcessById(procId);
p.Kill();
}
catch (Exception)
{
areTimedOut = false;
}
areTimedOut = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment