C
C#3y ago
yatta

❔ How to set a time interval in a timer ?

This is the class:
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.
27 Replies
ero
ero3y ago
freq is what? Like what are you actually entering? Just 1?
yatta
yattaOP3y ago
it take 3 argument is ProcessName, MaximumLifeTime, Frequency. After an interval time Frequency, the code will check if the Process running time has exceed its MaximumLifeTime and if it does then the code will kill the process for example program.exe notepad 2 3. It means every 3 minutes the code will check if notepad has run more than 2 minutes or not and if it does it will be killed but when I run my code, it seems take longer than the input time
Anchy
Anchy3y ago
store the Frequency and MaxLifeTime like you have but also store the TimeElapsed that you can compare against the MaxLifeTime before killing the process TimeElasped += Frequency, then you can check to see if TimeElapsed >= MaxLifeTime
yatta
yattaOP3y ago
I should add this into my main ?
Anchy
Anchy3y ago
something like this
string name = args[0];
int maxLifeTime = Convert.ToInt32(args[1]);
int freq = Convert.ToInt32(args[2]);
int timePassed = 0;
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) =>
{
if(timePassed >= maxLifeTime)
{
killer.KillProcess();
}
else
{
timePassed += freq;
}
};
string name = args[0];
int maxLifeTime = Convert.ToInt32(args[1]);
int freq = Convert.ToInt32(args[2]);
int timePassed = 0;
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) =>
{
if(timePassed >= maxLifeTime)
{
killer.KillProcess();
}
else
{
timePassed += freq;
}
};
although you are already doing that in KillProcess() so I may be confused on what you are trying to do here oh I see you want to check the process run time and not the run time that has passed since receiving the arguments my bad
yatta
yattaOP3y ago
ye, I was about to explain
Anchy
Anchy3y ago
right so var timer = new System.Timers.Timer(freq * 60000); means it will run timer.Elapsed += (s, e) => every 3 minutes if you are passing in 3 as the freq is that what you are expecting?
yatta
yattaOP3y ago
yes but it seems take longer than the expecting time
Anchy
Anchy3y ago
how have you come to that conclusion? how much longer?
yatta
yattaOP3y ago
i've tried with 1 minute and it seems take around 2 minute, give or take few seconds I also have the code to write down the record
Anchy
Anchy3y ago
that's odd, testing your code it runs how it should
yatta
yattaOP3y ago
hmmm maybe because of my hardware ?
Anchy
Anchy3y ago
the only thing I see which is extra odd is you are writing one minute has passed in your elapsed event when that may not be true elapsed will run (freq * 60000), it doesn't necessarily run every minute
yatta
yattaOP3y ago
just to check every minute passed when I run the program
Anchy
Anchy3y ago
yes, elapsed however will run every x milliseconds that you define in your constructor, if you want to check every minute then you need to define one minute or use another Timer
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;
}
};
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;
}
};
yatta
yattaOP3y ago
so how can i define it as minute ?
Anchy
Anchy3y ago
if freq = 3 then your elapsed will run every 3 minutes and your WriteLine will print every 3 minutes
yatta
yattaOP3y ago
ye, its what i'm expected
Anchy
Anchy3y ago
alright, I just thought it was odd you write "One minute passed" when that may not be the case if freq does not equal 1 is all
yatta
yattaOP3y ago
so i guess the long time i received is because of my hardware
Anchy
Anchy3y ago
I don't know
yatta
yattaOP3y ago
if so, there's nothing need to change it this situation i guess so can I ask an addition question here ?
Anchy
Anchy3y ago
var processName = "Notepad";
var maxLifeTime = 10;
var frequency = 1;

var timer = new System.Timers.Timer(frequency * 1000)
{
Enabled = true,
AutoReset = true
};

timer.Elapsed += (s, e) =>
{
foreach(var p in Process.GetProcessesByName(processName))
{
var time = DateTime.Now - p.StartTime;

if(time.TotalSeconds >= maxLifeTime)
{
Console.WriteLine("Exceeded Max Life Time.");
p.Kill();
}
}
};
timer.Start();
var processName = "Notepad";
var maxLifeTime = 10;
var frequency = 1;

var timer = new System.Timers.Timer(frequency * 1000)
{
Enabled = true,
AutoReset = true
};

timer.Elapsed += (s, e) =>
{
foreach(var p in Process.GetProcessesByName(processName))
{
var time = DateTime.Now - p.StartTime;

if(time.TotalSeconds >= maxLifeTime)
{
Console.WriteLine("Exceeded Max Life Time.");
p.Kill();
}
}
};
timer.Start();
this works perfectly on my end without any issue
yatta
yattaOP3y ago
what is the Task.Delay doing ?
Anchy
Anchy3y ago
thats just part of my testbench to delay the application from exiting just ignore it i removed the irrelevant code
yatta
yattaOP3y ago
tks for the reference tho, I'll check it
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server