Skip to content

Instantly share code, notes, and snippets.

@renanyoy
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renanyoy/d499e4ea7ce24cafddf3 to your computer and use it in GitHub Desktop.
Save renanyoy/d499e4ea7ce24cafddf3 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
namespace KillX2Go
{
public class Proc
{
public Process p;
TimeSpan ocpu ;
DateTime ot;
int overheat = 0;
public Proc(Process p)
{
this.p = p;
ocpu = p.TotalProcessorTime;
ot=DateTime.Now;
}
public void Check()
{
var ncpu= p.TotalProcessorTime;
var nt=DateTime.Now;
var u = (ncpu-ocpu).TotalSeconds / nt.Subtract(ot).TotalSeconds;
ocpu=ncpu;
ot=nt;
string killed = "";
if (u > 0.99)
{
overheat++;
if (overheat > 5)
{
p.Kill();
killed = " killed!";
}
}
else
overheat = 0;
Console.WriteLine(p.Id + " " + u + killed);
}
public bool HasExited
{
get
{
return p.HasExited;
}
}
public int Id
{
get
{
return p.Id;
}
}
}
class Program
{
static void Main(string[] args)
{
var proc = new Dictionary<int, Proc>();
while (true)
{
foreach (var p in Process.GetProcessesByName("x2goclient"))
{
if (!proc.ContainsKey(p.Id))
proc.Add(p.Id, new Proc(p));
}
Thread.Sleep(1000);
var del = new List<Proc>();
foreach (var p in proc.Values)
{
if (p.HasExited)
del.Add(p);
else
p.Check();
}
foreach (var p in del)
proc.Remove(p.Id);
Thread.Sleep(1000);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment