70 lines
1.5 KiB
C#
70 lines
1.5 KiB
C#
using System.Diagnostics;
|
|
|
|
namespace LeagueAPI.Utils;
|
|
|
|
public class ProcessWatcher : IDisposable
|
|
{
|
|
private bool _isDisposed;
|
|
private Timer _timer;
|
|
|
|
public bool IsGameRunning { get; private set; } = false;
|
|
|
|
public event EventHandler? LeagueOfLegendsExeFound;
|
|
|
|
public TimeSpan Interval
|
|
{
|
|
get => field;
|
|
set
|
|
{
|
|
if (field != value)
|
|
{
|
|
field = value;
|
|
_timer?.Change(TimeSpan.Zero, value);
|
|
}
|
|
}
|
|
} = TimeSpan.FromSeconds(10);
|
|
|
|
public ProcessWatcher()
|
|
{
|
|
_timer = new(OnTimerTick, null, TimeSpan.Zero, Interval);
|
|
}
|
|
|
|
private void OnTimerTick(object? state)
|
|
{
|
|
if (Process.GetProcessesByName("League of Legends").Length != 0)
|
|
{
|
|
if (!IsGameRunning)
|
|
{
|
|
LeagueOfLegendsExeFound?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
IsGameRunning = true;
|
|
}
|
|
else
|
|
{
|
|
IsGameRunning = false;
|
|
}
|
|
}
|
|
|
|
#region IDisposable
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (!_isDisposed)
|
|
{
|
|
if (disposing)
|
|
{
|
|
_timer?.Dispose();
|
|
}
|
|
|
|
_isDisposed = true;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
Dispose(disposing: true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
#endregion
|
|
}
|