❔ How to set a time interval in a timer ?
This is the class:
And this is the Main class that calls the class
I have my program above to check after an interval time, if a process have running longer than the allowed duration or not, and if it is the program will kill the process. The problem I got now is the time take pretty long than I expected. This is the first time I use the timer so Im sure how to adjust the time with it.
public string ProcessName { get; set; }
public int MaximumLifeTime { get; set; }
public int Frequency { get; set; }
public ProcessKiller(string name, int maximumLifeTime, int frequency)
{
this.ProcessName = name;
this.MaximumLifeTime = maximumLifeTime;
this.Frequency = frequency;
}
public bool KillProcess()
{
TimeSpan lifeTime = default;
foreach (var process in Process.GetProcessesByName(ProcessName))
{
lifeTime = DateTime.Now - process.StartTime;
if (lifeTime.TotalMinutes >= MaximumLifeTime)
{
process.Kill();
WriteLog();
}
}
return true;
}public string ProcessName { get; set; }
public int MaximumLifeTime { get; set; }
public int Frequency { get; set; }
public ProcessKiller(string name, int maximumLifeTime, int frequency)
{
this.ProcessName = name;
this.MaximumLifeTime = maximumLifeTime;
this.Frequency = frequency;
}
public bool KillProcess()
{
TimeSpan lifeTime = default;
foreach (var process in Process.GetProcessesByName(ProcessName))
{
lifeTime = DateTime.Now - process.StartTime;
if (lifeTime.TotalMinutes >= MaximumLifeTime)
{
process.Kill();
WriteLog();
}
}
return true;
}And this is the Main class that calls the class
public class Program
{
public static int MinuteCount { get; set; }
public static void Main(string[] args)
{
string name = args[0];
int maxLifeTime = Convert.ToInt32(args[1]);
int freq = Convert.ToInt32(args[2]);
var killer = new ProcessKiller(name, maxLifeTime, freq);
//Rerun the function after every "intervalTime" minute
var timer = new System.Timers.Timer(freq * 60000);
timer.Elapsed += (s, e) =>
{
killer.KillProcess();
MinuteCount++;
if (MinuteCount == 1)
{
Console.WriteLine("One minute passed !");
MinuteCount = 0;
}
};
//timer.Elapsed += Timer_Elapsed;
timer.Enabled = true;
timer.AutoReset = true;
timer.Start();
Console.WriteLine("Press any key to stop the program");
Console.ReadKey();
}
}public class Program
{
public static int MinuteCount { get; set; }
public static void Main(string[] args)
{
string name = args[0];
int maxLifeTime = Convert.ToInt32(args[1]);
int freq = Convert.ToInt32(args[2]);
var killer = new ProcessKiller(name, maxLifeTime, freq);
//Rerun the function after every "intervalTime" minute
var timer = new System.Timers.Timer(freq * 60000);
timer.Elapsed += (s, e) =>
{
killer.KillProcess();
MinuteCount++;
if (MinuteCount == 1)
{
Console.WriteLine("One minute passed !");
MinuteCount = 0;
}
};
//timer.Elapsed += Timer_Elapsed;
timer.Enabled = true;
timer.AutoReset = true;
timer.Start();
Console.WriteLine("Press any key to stop the program");
Console.ReadKey();
}
}I have my program above to check after an interval time, if a process have running longer than the allowed duration or not, and if it is the program will kill the process. The problem I got now is the time take pretty long than I expected. This is the first time I use the timer so Im sure how to adjust the time with it.
