I am having a console application written in c#, which is scheduled to run every 1 hr .
The application normally takes less than 10 min .
If the application is running more than 15 min I want to receive an email from the application with out breaking the code .
what is the best way to start with the minimum code .
You could set up a timer in your constructor
System.Timers.Timer aTimer = new System.Timers.Timer();
public static void Main()
{
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 1000 * 60 * 15; //1 second * 60 seconds in a minute * 15 minutes
aTimer.Enabled = true;
}
And then in the time elapsed event run your code which sends an email
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
//Code which sends an email
aTimer.Enabled = false;
}
Make a timer that runs for 15 minutes.
After the period Ends, it should check for a condition (finished, yes no)
if not finished -> mail.
Related
I am writing my first windows service and to start with it's just writing to a log file, which is working perfectly. I actually need the service to loop through the entries in a json file, which I have used elsewhere with no issues. So my service initiates a timer, and fires an event every 60 seconds;
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 60000; // 60 seconds
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
timer.Start();
This works absolutely perfectly and my OnTimer event is thus;
public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
{
eventLog1.WriteEntry("Drive Space monitoring initiated", EventLogEntryType.Information, eventId++);
}
This puts the entry in the log exactly as I expect every 60 seconds. My problem comes when I try to add a loop to the OnTimer event. For simplicity, here is what I added;
int count = 4;
for(int x = 1; x <= count; x++)
{
string msg = "This is just a counter: " + x.ToString();
eventLog1.WriteEntry(msg, EventLogEntryType.Information, eventId++);
}
This has absolutely no effect and no further entries are added to my log file when the OnTimer event fires.
Use System.Threading.Timer instead of System.Timers.Timer.
System.Timers.Timer gives issue some time when used in windows service and it will swallow all the exceptions.
I'm new in asp.net. Im develping a web-baised application that should prevent the user if he tried to login three times with wrong password.
I will disable the login button for 10 minutes then I will enable it.
this is the interface
and this is the timer code
protected void timer1_tick(object sender, EventArgs e)
{
timer--;
if (timer == 0)
{
Button1.Enabled = true;
Timer1.Enabled = false;
Label1.Visible = false;
}
}
but when I run the application, after 10 minutes it's refresh the page without enable the login button
If you are using a System.Tmers.Timer then simply call:
Timer1.Start();
If you are using a System.Threading.Timer then this should start immediately. The third argument in the constructor is the dueTime which is:
The amount of time to delay before callback is invoked, in milliseconds. Specify Timeout.Infinite to prevent the timer from starting. Specify zero (0) to start the timer immediately.
Source
So if this is non-zero your timer wont fire for the first time until after both the dueTime and period have elapsed. So if you have:
var timer1 = new Timer(callback, state, 10000, 10000);
the first time this will fire will be after 20 seconds and then it will fire every 10 seconds thereafter. If you want it to fire every 10 seconds then you need to specify 0 as the dueTime:
var timer1 = new Timer(callback, state, 0, 10000);
I have a windows service running with several timed functions:
_timer = new Timer(1 * 60 * 1000); // every 1 minute
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
_timer.Start();
_fbtimer = new System.Timers.Timer(48 * 60 * 60 * 1000);
_fbtimer.Elapsed += new System.Timers.ElapsedEventHandler(fbtimer_Elapsed);
_fbtimer.Start();
//Weekley Snapshot
_ventimer = new System.Timers.Timer(2 * 60 * 1000);
_ventimer.Elapsed += new System.Timers.ElapsedEventHandler(ventimer_Elapsed);
_ventimer.Start();
//Weekly Activity
_tevtimer = new System.Timers.Timer(2 * 60 * 1000);
_tevtimer.Elapsed += new System.Timers.ElapsedEventHandler(tevtimer_Elapsed);
_tevtimer.Start();
How do i set the timer to occur say once a week, or even better set it to a specific time on one day a week, without using quatrz/windows scheduler or a too different method.
Addition: this is how i am running a task everyday at 10
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//log.Info("Info - Check time");
DateTime startAt = DateTime.Today.AddHours(10);
if (_lastRun < startAt && DateTime.Now >= startAt)
{
_timer.Stop();
//Stuff
_lastRun = DateTime.Now;
_timer.Start();
}
}
without using quatrz/windows scheduler or a too different method.
Why reinvent the wheel, poorly? Anyway a weekly timer won't do much good. What if you code the timer to wait a week, and the machine reboots halfway the week for updates? The timer will start waiting a week again.
You'd better let it run at a small interval and check each time whether it's time to run the method you want. This way you can pre-calculate the 'event time' and check for it each time.
How do I get System.Timers.Timer to trigger Elapsed events every 15 mins in sync with the system clock? In other words, I want it to trigger exactly at xx:00, xx:15, xx:30, xx:45 (where xx means any hour)
You could let it elapse every second and check whether the current time is 00, 15, 30 or 45 and only then forward the event.
A first idea would be:
private static System.Timers.Timer aTimer;
private static System.DateTime _last;
public static void Main()
{
aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 1000;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
DateTime time =
new DateTime( 1,1,1, DateTime.Now.Hours, DateTime.Now.Minute );
if( time.Minute==0 ||time.Minute==15 || time.Minute==30 || time.Minute==45 )
{
// Avoid multiple notifications for the same quarter.
if ( _last==DateTime.MinValue || _last!=time )
{
_last = time;
// Do further processing.
doProcessing();
}
}
}
(Example based on this MSDN documentation)
When starting the program, or changing the event times that will be triggered, load the event times into memory (to keep from reading this data from the hard drive every second.) Then set up a timer to fire every 1 second. A timer set to fire every 1 second is very little overhead on the processor. Set one up and open task manager and you will not even notice the processor running any more than before the timer was running. Then put a check in the timer event to check if it is time to fire an event.
use Quartz.net. Then you can use regex to define the interval.
I need to run a function every 5 seconds for 10 minutes.
I use a timer to run it for 5 secs, but how do I limit the timer to only 10 mins?
Just capture the time that you want to stop and end your timer from within the elapsed handler. Here's an example (note: I used a System.Threading.Timer timer. Select the appropriate timer for what you are doing. For example, you might be after a System.Windows.Forms.Timer if you are writing in Winforms.)
public class MyClass
{
System.Threading.Timer Timer;
System.DateTime StopTime;
public void Run()
{
StopTime = System.DateTime.Now.AddMinutes(10);
Timer = new System.Threading.Timer(TimerCallback, null, 0, 5000);
}
private void TimerCallback(object state)
{
if(System.DateTime.Now >= StopTime)
{
Timer.Dispose();
return;
}
// Do your work...
}
}
Have your timer loop something like this:
DateTime endTime = DateTime.Now.AddMinutes(10);
while(endTime < DateTime.Now)
{
// Process loop
}
Divide the Y minutes by the X interval to get how many times it needs to run. After that you just need to count how many times the function has been called.
In your case, 10 min = 600 seconds / 5 seconds = 120 calls needed. Just have a counter keep track of how many times your function has been called.
Timer.Stop() after 120 Ticks.
just use a DateTime variable to track when it should end and set that right before you start. The on your Elapsed event handler, check if the signal time is less than the end time. If it isn't, stop the timer.
You can calculate how times your function will be call, and create decrement counter, after elapsed which you unsubscribe from timer tick. Or you can Run another timer which have tick period - 10 min and on tick you unsubscribe from timer tick calling your function.
Note the start time. In each call, test if currentTime + 5 seconds > startTime + 10 minutes. If so, disable the timer.
I prefer this approach to just running for N ticks, as timers are not guaranteed to fire when you'd like them to. It's possible 120 ticks may run over 10 minutes of real world time.
You can set two timers one that run for 5 secs and the other one that run for 10min and disable the first one
You could use a second timer:
class Program
{
static void Main(string[] args)
{
int interval = 5 * 1000; //milliseconds
int duration = 10 * 60 * 1000; //milliseconds
intervalTimer = new System.Timers.Timer(interval);
durationTimer = new System.Timers.Timer(duration);
intervalTimer.Elapsed += new System.Timers.ElapsedEventHandler(intervalTimer_Elapsed);
durationTimer.Elapsed += new System.Timers.ElapsedEventHandler(durationTimer_Elapsed);
intervalTimer.Start();
durationTimer.Start();
}
static void durationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
intervalTimer.Stop();
durationTimer.Stop();
}
static void intervalTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//call your method
}
private static System.Timers.Timer intervalTimer;
private static System.Timers.Timer durationTimer;
}