using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
using System****;
using System.Reflection;
namespace Idle_Master
{
public delegate void DogStateChangeD(GameProfile profile, DogState state, bool change);
public enum DogState { Running, Idle };
class WatchDog
{
private GameProfile _profile = null;
private Process _process = null;
//
private string FileNameNoExt = null;
private Thread IdleChecker = null;
private System.Threading.Timer StartWatcher;
//
public event DogStateChangeD DogStateChange;
public WatchDog(GameProfile profile_) : this(profile_, null) { }
public WatchDog(GameProfile profile_, Process process_)
{
_profile = profile_;
if (process_ != null) { _process = process_; }
//
FileNameNoExt = Path.GetFileNameWithoutExtension(_profile.Path);
StartWatcher = new Timer(new TimerCallback(StartWatcher_Tick));
IdleChecker = new Thread(new ThreadStart(threadedIdleWait));
//
if (isRunning) { IdleChecker.Start(); DogStateChange(_profile, DogState.Running, true); }
else { StartWatcher.Change(250, 1000); DogStateChange(_profile, DogState.Running, false); }
}
private void StartWatcher_Tick(object state)
{
if (isRunning) { IdleChecker.Start(); DogStateChange(_profile, DogState.Running, true); }
}
private void _process_Exited(object sender, EventArgs e)
{
IdleChecker.Abort();
StartWatcher.Change(250, 1000);
DogStateChange(_profile, DogState.Running, false);
}
public GameProfile Profile { get { return _profile; } }
public Process Game_Process
{
get { return _process; }
set { _process = value; }
}
public bool isRunning
{
get
{
if (_process == null)
{
Process[] runningProcs = Process.GetProcessesByName(FileNameNoExt);
if (runningProcs.Length > 0)
{
_process = runningProcs[0];
_process.Exited += _process_Exited;
return true;
}
else { return false; }
}
else { return (_process.HasExited) ? false : true; }
}
}
private void threadedIdleWait()
{
DateTime idleTime = new DateTime(1, 1, 1);
while (1 == 1)
{
if (_process.WaitForInputIdle(0))
{
if (idleTime.Year == 1)
{
idleTime = DateTime.Now;
}
else
{
if (DateTime.Now.Subtract(idleTime).TotalSeconds > 2) //TODO: > Setting*****leTime
{
DogStateChange(_profile, DogState.Idle, true);
}
}
}
else
{
idleTime = new DateTime(1, 1, 1);
DogStateChange(_profile, DogState.Idle, false);
}
Thread.Sleep(500);
}
}
}
public class DogWatcher
{
List<WatchDog> WatchList = new List<WatchDog>();
//
public event DogStateChangeD DogStateChange;
public void Add(GameProfile gp)
{
WatchList.Add(new WatchDog(gp));
WatchList[WatchLis*****unt - 1].DogStateChange += DogWatcher_DogStateChange;
}
private void DogWatcher_DogStateChange(GameProfile profile, DogState state, bool change)
{
DogStateChange(profile, state, change);
}
}
}
ThreadPool.QueueUserWorkItem(delegate{
if(DogStateChanged != null)
{
//call the event...
}
});
public event DogStateChangeD DogStateChange = delegate { };
WatchList[WatchLis*****unt - 1].DogStateChange += DogWatcher_DogStateChange;

#region GameProfile (object)
public class GameProfile
{
private string _name;
private string _path;
public GameProfile(string name_, string path_)
{
_name = name_;
_path = path_;
}
public string Name
{
get { return _name; }
}
public string Path
{
get { return _path; }
}
}
#endregion
private bool exit = false;
private void threadedIdleWait()
{
DateTime idleTime = new DateTime(1, 1, 1);
while (1 == 1)
{
if(exit)
{
Thread.CurrentThread.Abort();
} else
{
if (_process.WaitForInputIdle(0))
{
if (idleTime.Year == 1)
{
idleTime = DateTime.Now;
}
else
{
if (DateTime.Now.Subtract(idleTime).TotalSeconds > 2) //TODO: > Setting*****leTime
{
DogStateChange(_profile, DogState.Idle, true);
}
}
}
else
{
idleTime = new DateTime(1, 1, 1);
DogStateChange(_profile, DogState.Idle, false);
}
Thread.Sleep(500);
}
}
}
}
private void _process_Exited(object sender, EventArgs e)
{
exit = true;
StartWatcher.Change(250, 1000);
DogStateChange(_profile, DogState.Running, false);
}
public void Add(GameProfile gp)
{
try
{
WatchList.Add(new WatchDog(gp));
WatchList[WatchLis*****unt - 1].DogStateChange += DogWatcher_DogStateChange;
}
catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Idle_Master
{
public partial class MainForm : Form
{
DogWatcher Leashes;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
Leashes = new DogWatcher();
Leashes.DogStateChange += Leashes_DogStateChange;
//
listView1.Items.Add(new ListViewItem(new string[] { "Chromium Updater", "True", "False" }));
Leashes.Add(new GameProfile("Chromium Updater", "C:\\Chromium Updater.exe"));
}
private void Leashes_DogStateChange(GameProfile profile, DogState state, bool change)
{
if (InvokeRequired)
{
BeginInvoke(new DogStateChangeD(Leashes_DogStateChange), new object[] { profile, state, change });
return;
}
//
foreach (ListViewItem item in listView1.Items)
{
if (item.SubItems[0].Text == profile.Name) { item.SubItems[2].Text = change.ToString(); break; }
}
}
private void button4_Click(object sender, EventArgs e)
{
//Settings
}
}
}
