Skip to content

Instantly share code, notes, and snippets.

@chgeuer
Created April 19, 2012 13:24
Show Gist options
  • Save chgeuer/2420964 to your computer and use it in GitHub Desktop.
Save chgeuer/2420964 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
var cts = new CancellationTokenSource();
var ct = cts.Token;
var tasks = new List<Task>();
Action bumm = () =>
{
var process = Process.Start(@"c:\Windows\System32\cmd.exe", "/k");
if (process == null) throw new NotSupportedException();
while (true)
{
if (ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(1)))
{
process.Kill();
return;
}
}
};
var t = new Task(bumm, ct);
t.Start();
tasks.Add(t);
tasks.Add(Task.Factory.StartNew(bumm, ct));
try
{
Console.ReadKey();
cts.Cancel();
Task.WaitAll(tasks.ToArray());
}
catch (AggregateException ae)
{
foreach (Exception e in ae.InnerExceptions)
Console.Error.WriteLine(e.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment