I would like to clarify possible code implementaion in case if the code executing withing timerDoJob_Elapsed is getting longer that it should.
So all internal things are gonna be coruppted/canceled and etc when timer fires next time.
Is there any pattern for this kind of problem?
Any clue about make it more stable?
private void timerDoJob_Elapsed(object sender, ElapsedEventArgs e)
{
VeryLongTask1();
VeryLongTask2();
VeryLongTask3();
}
If you're using System.Timers.Timer, turn off AutoReset. Then re-enable the timer at the end of your Elapsed method.
private void timerDoJob_Elapsed(object sender, ElapsedEventArgs e)
{
VeryLongTask1();
VeryLongTask2();
VeryLongTask3();
mytimer.Enabled = true;
}
This way, the timer does not even start again until the task is complete. But make sure to wrap everything in your Elapsed method in try/catch blocks because if an unhandled exception happens there, your timer won't start again.
Another method I've used before is to use a lock in your Elapsed event:
var myLock = new Object();
private void timerDoJob_Elapsed(object sender, ElapsedEventArgs e)
{
lock (myLock) {
VeryLongTask1();
VeryLongTask2();
VeryLongTask3();
}
}
This way, two instances of the Elapsed event cannot run at the same time. However, there is a caveat: If the task takes longer than two or more iterations of the timer, then you can have iterations adding up in the background. For example, if your timer elapses after 5 seconds, but your task takes 60 seconds to complete, by the time the first one finishes, you'll have 11 waiting.
Timer.Change method giving Timeout.Infinite and 0 as arguments will stop the timer (from MSDN):
If dueTime is zero (0), the callback method is invoked immediately. If
dueTime is Timeout.Infinite, the callback method is never invoked; the
timer is disabled, but can be re-enabled by calling Change and
specifying a positive value for dueTime.
One possible and simple but yet powerful approach is stopping the timer until the whole tasks have ended, and then, start the timer again:
private void timerDoJob_Elapsed(object sender, ElapsedEventArgs e)
{
timerDoJob.Change(Timeout.Infinite, 0);
// Use a try-finally so if some tasks throws an exception
// the timer will be re-enabled again anyway
try
{
VeryLongTask1();
VeryLongTask2();
VeryLongTask3();
}
finally
{
timerDoJob.Change(0, 5000);
}
}
Related
How do I execute an infinite loop in C# with a 1 minute delay per iteration?
Is there any way to do it without using some kind of variable with x++ and setting x to some incredibly large number?
Solution1 :
If you want to wait for 1 minute without hanging your Main Thread, it is good to use Timer Control.
Step 1: You need to Subscribe to the Timer Tick event.
Step 2: Set the Interval property of the Timer to 60000 milliseconds for raising the event for every Minute.
Step 3: In Tick Event Handler just do ehatever you want to perform.
Step 4: you can Call the timer1.Stop() method whenever you want to stop the timer.
Note : if you don't stop the timer it becomes infinite.
if you want to stop the timer you can call timer1.Stop();
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=60000;//one minute
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();
private void timer1_Tick(object sender, EventArgs e)
{
//do whatever you want
}
Solution 2:
EDIT : From the below comments : if the OP(Original Poster) is Trying to run this from Console Application System.Timers.Timer can be used
Note : instead of Handling Tick Event , OP has to handle the Elapsed Event.
Complete Code:
class Program
{
static System.Timers.Timer timer1 = new System.Timers.Timer();
static void Main(string[] args)
{
timer1.Interval = 60000;//one minute
timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick);
timer1.Start();
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
static private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e)
{
//do whatever you want
Console.WriteLine("I'm Inside Timer Elapsed Event Handler!");
}
}
while (true)
{
System.Threading.Thread.Sleep(60000);
}
Now if we assume you don't want this thread to block and you're ok dealing with threading concerns, you can do something like this:
System.Threading.Tasks.Task.Run(() =>
{
while (true)
{
// do your work here
System.Threading.Thread.Sleep(60000);
}
});
The Task will put your work on a ThreadPool thread, so it runs in the background.
You can also look at a BackgroundWorker if that's more geared toward what you want.
for(;;)
{
//do your work
Thread.Sleep(60000);
}
This is not optimal but does exactly what it's asked.
From a similar question on MSDN:
>
System.Threading.Thread.Sleep(5000);
this codes make your application waiting for 5 seconds.
Change the number as necessary for the amount of time you want to sleep for (for one minute, this would be 60000).
You can put this where you want in your while loop
while(true){
Sleep(60000);}
This would be a blocking call, so you would want to put it on its own thread or any kind of UI that you would have would hang badly.
Sleep is in the System.Threading.Thread namespace.
I have a while loop running in my .NET backgroundworker. I would like to end the loop when Timers.Timer reaches 0.
Problem is that since I'm working in another thread (backgroundworker), my timer has to be instantiated in that same thread. So I can't set any private boolean timer_Elapsed. Nether do I know how to give reference of boolean thro event.
Code Example:
private bool timer_Elapsed = false;
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
System.Timers.Timer timer = new System.Timers.Timer();
Set_Timer(timer);
timer.Start();
while(timer_Elapsed) //Has to be a boolean that indicates if timer elapsed
{
this.Do_Proces();
}
}
private void Set_Timer(System.Timers.Timer timer)
{
timer.Interval = 200;
timer.Elapsed += new ElapsedEventHandler(timer_ElapsedEvent);
}
private void timer_ElapsedEvent(object source, ElapsedEventArgs e)
{
timer_Elapsed = true; //I can't set my private boolean since it got instantiated in another thread
}
Particular questions in code. I'm new with this kind of stuff.
Any suggestions? Thanks in advance
EDIT: To clarify, I want the Do_Proces() to run for 200 milliseconds, when that time passed, I want it to stop. When it stops after 200 millisec, I want to and update GUI with data generated in backgroundWorker. Then check if user wants the proces to stop, if not, I want it to run again.. I use the timer because the thread will have to get restarted to much, this will have effect on the main thread as well, effecting the user experience badly.
Is the timer serving any other purpose other than listed here? If not, you may just want to record the current time at the beginning of your BackgroundWorker method, and change the condition on the while loop to check if the required amount of time has elapsed.
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
TimeSpan timeout = TimeSpan.FromMinutes(5);
DateTime start_time = DateTime.Now;
while(DateTime.Now - start_time < timeout)
{
this.Do_Proces();
}
}
I am having a problem in running a backgroundworker on a given specific time.
My code runs the backgoundworker in only one second.
I want to increase the Interval time in my background.
I am using this line of code to run a background in a button click Event:
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
Then in backgroundWorker1_DoWork:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker1.CancelAsync();
}
At last in backgroundWorker1_RunWorkCompleted:
private void backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
I want to run the background continuously but for every 5 seconds or more than 5.
There would be a great appreciation if someone could help me,
Thanks in advance.
Run-time
Timer class represents a Timer control and used to create a Timer at run-time. The following code snippet creates a Timer at run-time, sets its property and event handler.
Timer t = new Timer();
t.Interval = 2000;
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(OnTimerEvent);
The event handler code looks like following.
private void OnTimerEvent(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
Here is demo : C# Timer Tutorial
Check documentation on msdn : http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
you could add a Timer setting the interval to how often you want the background worker to run and on the timers elapsed event you could start your background worker.
you will want to check that the background worker is not busy before you attempt to start it again though. If this situation occurs then you might consider immediately starting the background worker again when it completes. (if you want it to run at least once every 5 secs)
If you want it to wait 5 seconds after completion, then you need to stop the timer before you start the background worker, then in the background workers completed event you need to reset the timer and start it again.
EDIT
after one of your comments below it seems that you have many backgroundworkers, in which case using one of the other approaches which inserts a delay in the background workers completed event before starting the backgroundworker again is probably a better solution.
You could insert the delay using Thread.Sleep() as has been suggested or you could maybe create a timer in the function and assign a delegate to the timers elapsed event which restarted the background worker. Something along these (untested) lines:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Timer timer = new Timer();
timer.Interval = 5000;
timer.Enabled = true;
timer.Elapsed+=delegate (object sender, ElapsedEventArgs args)
{
backgroundWorker1.RunWorkerAsync();
};
timer.Start ();
}
Do you need it to run exactly every five seconds or not more often than five seconds? If it's the latter you could call Sleep(5000) on the Thread.CurrentThread just before your BackgroundWorker finishes its DoWork() method.
The easiest solution would be to let the thread sleep for 5 seconds in the beginning of backgroundWorker1_DoWork: Thead.Sleep(5000).
Alternatively, you can set a timer in RunWorkerCompleted that expires in 5 seconds and then starts the BackgroundWorker again.
If you want to use BW try this:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Thread.Sleep(5000);//wait for 5s then run it again
backgroundWorker1.RunWorkerAsync();
}
Couldn't you use a Timer to start your BackgroundWorker?
I created a windows service, that is supposed to check a certain table in the db for new rows every 60 seconds. For every new row that was added, I need to do some heavy processing on the server that could sometimes take more than 60 seconds.
I created a Timer object in my service, that ticks every 60 seconds and invokes the wanted method.
Since I don't want this timer to tick while processing the new lines found, I wrapped the method in a lock { } block, so this won't be accessible by another thread.
It looks something like this :
Timer serviceTimer = new Timer();
serviceTimer.Interval = 60;
serviceTimer.Elapsed += new ElapsedEventHandler(serviceTimer_Elapsed);
serviceTimer.Start();
void serviceTimer_Elapsed(object sender, ElapsedEventArgs e)
{
lock (this)
{
// do some heavy processing...
}
}
Now, I'm wondering -
If my timer ticks, and finds a lot of new rows on the db, and now the processing will take more than 60 seconds, the next tick won't do any processing till the previous one finished. This is the effect I want.
But now, will the serviceTimer_Elapsed method go off immediatly once the first processing was finished, or will it wait for the timer to tick again.
What I want to happen is - if the processing requires more than 60 seconds, than the timer will notice the thread is locked, and wait another 60 seconds to check again so I will never get stuck in a situation where there are a queue of threads waiting for the previous one to finish.
How can i accomplish this result ?
What is the best practice for doing this ?
Thanks!
You might try disabling the timer during processing, something like
// Just in case someone wants to inherit your class and lock it as well ...
private static object _padlock = new object();
try
{
serviceTimer.Stop();
lock (_padlock)
{
// do some heavy processing...
}
}
finally
{
serviceTimer.Start();
}
Edit : OP didn't specify whether the reentrancy was caused only by the timer or whether the service was multi threaded. Have assumed the later, but if the former then locking should be unnecessary if the timer is stopped (AutoReset or manually)
You don't need the lock in this case. Set timer.AutoReset=false before starting it.
Restart the timer in the handler after you are done with your processing. This will ensure that the timer fires 60 seconds after each task.
A similar variation on other answers, that allows the timer to keep ticking and only do the work when the lock can be obtained, instead of stopping the timer.
Put this in the elapsed event handler:
if (Monitor.TryEnter(locker)
{
try
{
// Do your work here.
}
finally
{
Monitor.Exit(locker);
}
}
Put a quick check it see if the service is running. if it is running it will skip this event and wait for the next one to fire.
Timer serviceTimer = new Timer();
serviceTimer.Interval = 60;
serviceTimer.Elapsed += new ElapsedEventHandler(serviceTimer_Elapsed);
serviceTimer.Start();
bool isRunning = false;
void serviceTimer_Elapsed(object sender, ElapsedEventArgs e)
{
lock (this)
{
if(isRunning)
return;
isRunning = true;
}
try
{
// do some heavy processing...
}
finally
{
isRunning = false;
}
}
I recommend you don't let the timer tick at all while its processing.
Set the Timers AutoReset to false. And start it at the end. Here's a full answer you might be interested in
Needed: A Windows Service That Executes Jobs from a Job Queue in a DB; Wanted: Example Code
Other options might be to use a BackGroundWorker class, or TheadPool.QueueUserWorkItem.
Background worker would easily give you the option check for current processing still occurring and process 1 item at a time. The ThreadPool will give you the ability to continue queueing items every tick (if necessary) to background threads.
From your description, I assume you are checking for items in a queue in a database. In this case, I would use the ThreadPool to push the work to the background, and not slow/stop your checking mechanism.
For a Service, I would really suggest you look at using the ThreadPool approach. This way, you can check for new items every 60 seconds with your timer, then Queue them up, and let .Net figure out how much to allocate to each item, and just keep pushing the items into the queue.
For Example: If you just use a timer and you have 5 new rows, which require 65 seconds of processing time total. Using the ThreadPool approach, this would be done in 65 seconds, with 5 background work items. Using the Timer approach, this will take 4+ minutes (the minute you will wait between each row), plus this may cause a back-log of other work that is queueing up.
Here is an example of how this should be done:
Timer serviceTimer = new Timer();
void startTimer()
{
serviceTimer.Interval = 60;
serviceTimer.Elapsed += new ElapsedEventHandler(serviceTimer_Elapsed);
serviceTimer.AutoReset = false;
serviceTimer.Start();
}
void serviceTimer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
// Get your rows of queued work requests
// Now Push Each Row to Background Thread Processing
foreach (Row aRow in RowsOfRequests)
{
ThreadPool.QueueUserWorkItem(
new WaitCallback(longWorkingCode),
aRow);
}
}
finally
{
// Wait Another 60 Seconds and check again
serviceTimer.Stop();
}
}
void longWorkingCode(object workObject)
{
Row workRow = workObject as Row;
if (workRow == null)
return;
// Do your Long work here on workRow
}
There's quite a neat way of solving this with Reactive Extensions. Here's the code, and you can read a fuller explanation here: http://www.zerobugbuild.com/?p=259
public static IDisposable ScheduleRecurringAction(
this IScheduler scheduler,
TimeSpan interval,
Action action)
{
return scheduler.Schedule(
interval, scheduleNext =>
{
action();
scheduleNext(interval);
});
}
And you could use it like this:
TimeSpan interval = TimeSpan.FromSeconds(5);
Action work = () => Console.WriteLine("Doing some work...");
var schedule = Scheduler.Default.ScheduleRecurringAction(interval, work);
Console.WriteLine("Press return to stop.");
Console.ReadLine();
schedule.Dispose();
another posibility would be something like this:
void serviceTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (System.Threading.Monitor.IsLocked(yourLockingObject))
return;
else
lock (yourLockingObject)
// your logic
;
}
Got a quick question on creating a C# thread.
It needs to run every 10 minutes
The worker will do work then go into sleep for another 10 minutes
It can also be triggered to run immediately by calling Trigger()
It can be stopped by calling Stop()
I've created one with ManualResetEvent, which is set when Stop() is called. This works well but do I need to create another ManualResetEvent or wait handle in order to be able to trigger the worker immediately?
If by Stop you mean Stop waiting and don't run again then I think a Threading.Timer will be a good (lean) choice.
You can activate a timer with a DoWork() method and a 10 minutes interval. It will use the ThreadPool which seems the best choice here. Trigger() can queue DoWork directly on the Pool and Stop() can deactivate the Timer. DoWork() shouldn't use Sleep() and be suitable for the ThreadPool.
Using a timer here makes sense. Here's a quick code-snippet which I haven't tested.
private System.Timers.Timer _timer = null;
public void Constructor()
{
_timer = new System.Timers.Timer(600000);
_timer.Elapsed += new ElapsedEventHandler(t_Elapsed);
}
public void ForceDoWork()
{
//unsubscribe to timer event, so work dowsnt get fired twice
_timer.Elapsed -= new ElapsedEventHandler(t_Elapsed);
StartWorking();
_timer.Elapsed += new ElapsedEventHandler(t_Elapsed);
}
public void StartWorking()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(WorkToBeDone));
}
private void WorkToBeDone(object state)
{
//work here
}
public void t_Elapsed(object sender, ElapsedEventArgs e)
{
StartWorking();
}
Yes, you'll need another wait handle to force thread to execute. You'll basically have to WaitOne for this handle for 10 minutes, thus either elapsing a timeout or continuing whenever the wait handle becomes signalled.
Don't forget to switch that handle to a non-signalled state, though.