how to recalculate something ~once per second? - c#

I need to do some pretty fast recalculation ~every second.
What is the best way to do that? Is using dedicated thread and Thread.Sleep is ok?
Task.Factory.StartNew(() =>
{
while (true)
{
RecalculateState();
Thread.Sleep(1000);
}
}, TaskCreationOptions.LongRunning);

That would work - but another alternative would be to use a timer, e.g. System.Threading.Timer or System.Timers.Timer.
You should think about:
What do you want to happen if it takes more than a second to recalculatte state?
Is your RecalculateState method entirely safe to be called from arbitrary threads?

You can use System.Timers.Timer with 1 second interval.
It already runs in a new thread .
Pay attention on fact, that if RecalculateState runs longer than expected interval (for 1000 of reasons) you have to deal with calls overlapping, so you have to manage that case in some way.
One of possible solutions, is to run a new code only after execution of the method finished, and measure difference between execution time and interval. But this is not so easy task to do. Fortunatelly someone already thought about that problem.
Can have a look on Reactive Extensions that in latest build payed special attention on time dependent execution.

Can't you use a timer and make a ontimed event?
Something like this ?
Observable.Timer(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1)).Timestamp()
.Subscribe(MyOnTimedEventMethod);

I would suggest to use a timer for this.
member:
private readonly Timer _timer;
instantiate the timer in the constructor for instance:
_timer= new Timer(OnTimerEllapsed, null, 0, 1000);
callback:
private void OnTimerEllapsed(object sender)
{
RecalculateState();
}

Related

Dispose Timer inside of anonymous method

I have a block of code that will be called relatively often. Prior to it being called I need a 2000ms delay to take place.
The first thing that has come to mind is creating/disposing of a timer every time the method is called.
To accomplish this I'm using a Timer (see code). My question is...any dangers/problems calling Dispose inside of the anonymous method below? Recommendations of a better approach?
Are there any downsides to doing the following? Bad idea?
delayTimer = new Timer() { Interval = 2000 };
{
delayTimer.Tick += (sender2, e2) =>
{
((Timer)sender2).Stop();
MessageBox.Show("Do something after 2000ms");
delayTimer.Dispose();
};
}
One thing you can do differently is move the Dispose for the timer to the front of the anonymous method. This way, even if you throw an exception later in the method, you've still Disposed the timer. I've used this kind of pattern before and it's a reasonably clean way to get a delayed callback.
If you're using C#5, there is a very nice Task.Delay method you can await to get a timer callback within an async method. This is often used to implement timeouts in combination with a call to WaitAny like this:
public static async Task WithTimeout(this Task task, int timeout, string timeoutMessage = null, params object[] args)
{
var timeoutTask = Task.Delay(timeout);
if (await Task.WhenAny(task, timeoutTask) == timeoutTask)
throw new TimeoutException(timeoutMessage == null ? "Operation timed out" : string.Format(timeoutMessage, args));
await task;
}
Very strange task, but ok.
If you really need to do this, you really want to use a timer, and that block of code will really be called very often, than you should consider to use a cache of timers.
Each time the block of code is called, you should check the cache whether it contains a free timer. If yes, than just use the first available, otherwise, create a new one and put it into the cache. Also, you will need an another timer which will work all the time and will regularly check the cache for an unused timers. If some timer or timers are unused for lets say 10 seconds, than dispose it and remove from cache. Such approach will significantly reduce the number of created instances of timers.
You need a cache only in case if you create timers really very often (hundreds times per second), because multiple creation and disposing such heavy object as Timer (it uses some system resources) can lead to a performance issue. But from your detailed description it is obvious that you are not going to create timers so often, so you can keep your solution. But instead of using timer, you can use RX's Interval observable collection, and all your code will be shortened to 1 string:
Observable.Interval(TimeSpan.FromSeconds(2)).Take(1).Subscribe(_ => MessageBox.Show("Do something after 2000ms"));
I didn't have any serious performance issues creating a thousand timers with your code.
Disposing of a Timer (or any IDisposable) within an anonymous method is no problem at all.
However, your code isn't written in the best way possible. Try this implementation instead:
var delayTimer = new Timer()
{
Interval = 2000,
Enabled = true,
};
EventHandler tick = null;
tick = (_s, _e) =>
{
delayTimer.Tick -= tick;
delayTimer.Stop();
delayTimer.Dispose();
MessageBox.Show("Do something after 2000ms");
};
delayTimer.Tick += tick;
It's always a good idea to detach the event before trying to dispose of the timer. In fact there may be many times that failing to detach will not allow the GC to clean up properly and you could have a memory leak.
Nevertheless I do like the Rx answer to this question as the cleanest way to go. Although using Rx doesn't marshall the callback on the UI thread unless you do this:
Observable
.Timer(TimeSpan.FromSeconds(2.0))
.ObserveOn(this) // assuming `this` is your form
.Subscribe(_ => MessageBox.Show("Do something after 2000ms"));
Much simpler.

Where can I find a good C# console app that demonstrates polling?

I'm looking for a good example of polling in C#.
Basically, every X seconds an instrument is read and the values logged to a text file. I was looking for a sample that makes use of .NET 4's parallel library. Or, maybe I'm overthinking this solution by looking at TPL...
ps- this question is unrelated to my previous question about database polling.
I'm not sure I'd particularly bother with TPL here. Just use System.Threading.Timer or System.Timers.Timer to perform an action periodically. Those will both use the thread pool - what are you planning on doing in the main console thread during this time?
Of course, another extremely simple option would be to just make the main thread sleep between poll occurrences. It's crude, but if that's all your app needs to do, it may well be good enough for you. I'm not sure how it behaves if the system clock is changed, mind you... is this for a very long-running task for production usage, or just a quick tool? (If it's a long-running app, you might want to consider using a Windows Service instead.)
It's real easy to create a timer:
static void Main(string[] args)
{
// Create a timer that polls once every 5 seconds
var timer = new System.Threading.Timer(TimerProc, null, 5000, 5000);
Console.WriteLine("Polling every 5 seconds.");
Console.WriteLine("Press Enter when done:");
Console.ReadLine();
timer.Dispose();
}
static int TickCount = 0;
static void TimerProc(object state)
{
++TickCount;
Console.WriteLine("tick {0}", TickCount);
}
Note that the TimerProc is called on a separate thread. Your main program can do other things, and this will continue to poll every five seconds until you kill (or modify) the timer.
I prefer System.Threading.Timer over System.Timers.Timer because the latter swallows exceptions. If there is a bug in your elapsed event handler that throws an exception, you'll never know about it because the System.Timers.Timer event handler will suppress the exception. For that reason, I strongly suggest that you not use it. Use System.Threading.Timer instead.
Using the PL doesn't sound correct to me for this task. I recommend checking out System.Timer with which you can specify a recurring interval at which the Elapsed event is raised in your application allowing you to handle the event to perform regular processing.

What are the best practices for doing a task after a certain time interval?

I have a Windows Application. We have implemented AutoSave functionality as background process.
Sample code is as below:
While(1)
{
Thread.Sleep(60000) // 1 minute sleep
DoAutoSaveAllControls();
}
I think this is bad functionality. Correct me if I am wrong. But, I want to improve performance and do this task after certain time interval, without doing Sleep.
Also, is it good to do this in background process?
A much better approach would be to use a timer. You can find out about the various different timers in the .NET framework from this excellent article:
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
You are using WinForms, so a System.Windows.Forms.Timer will be just fine for you.
For example:
System.Windows.Forms.Timer tmrWindowsFormsTimer = new System.Windows.Forms.Timer();
tmrWindowsFormsTimer.Interval = TimeSpan.FromMinutes(1);
tmrWindowsFormsTimer.Tick += new EventHandler(tmrWindowsFormsTimer_Tick);
tmrWindowsFormsTimer.Start();
private void tmrWindowsFormsTimer_Tick(object sender, System.EventArgs e) {
tmrWindowsFormsTimer.Stop();
DoAutoSaveAllControls();
}
This stops the timer after the first tick, effectively a fire-once timer.
You can use Reactive Extenssions for this as well.It looks more natural and you can combine observables.
var observable = Observable.Timer(
TimeSpan.FromMinutes(1),
TimeSpan.FromMinutes(1)).Timestamp();
using (observable.Subscribe()))
{
DoAutoSave();
}
Thread.Sleep does not affect performance at all. In order to me is perfectly ok, but since your application is probably modifying the document in the UI thread you probably need to sincronize the save in order to avoid concurrent modifications. Just for this reason maybe it would be better to use a Timer instead of BackGroundWorker.
You're right, it's not really a good use of a thread. Take a look at the Timer class.
You can use System.Timers.Timer to start a process after certain interval, check the sample snippet
aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(YourHandlerMethod);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true;
I think you need to trigger save functionality from the calling code (that knows if any changes had already happaned). So that saving thread could know for sure that calling thread has made some changes to save.
This is not an answer for this question, just maybe recommendation. So if you are calling Save from inside of timer, you should verify first if any change happened. To do that you'll need some additional variable, that would be common for working thread and saving thread. If working thread did change something, it triggers that var to true. When saving, if var is true - then saving is needed. After saving - change common var to false.

What is the best way to wait for a certain time (say 10 seconds) in C#?

I want to wait for 15 seconds, then the control should resume from the next statement.
I don't have anything else to do while waiting (Just waiting).
I know that there is Thread.Sleep(15000). What I don't know is the best method to wait? What are the limitations of this?
The code would be like this:
Method()
{
statement 1;
statement 2;
//WaitFor 15 secs here;
statement 3;
}
The disadvantage of Thread.Sleep is if this is called in your GUI thread (the thread that processes GUI events, for example, a button click handler method, or a method called from a button click handler, etc.) then you application will appear to freeze and be nonresponsive for those 15 seconds.
It would be perfectly fine if you had explicetly created a seperate thread and called Thread.Sleep in it, assuming you don't mind that thread not doing anything for 15 seconds.
The alternative would be to create a Timer and start it after stmt 2, and place stmt 3 in the Tick event handler for the timer, and also stop the timer in that handler.
This may not be a direct answer to your question. I would say check whether your process flow is better than checking whether the code is better ;-)
Are you waiting for 15 seconds just to make sure stmt2; is complete? If so then adding an handler, as soon as stmnt 2 is executed, would be a better solution (?)
You can also use a timer to wait. Thread.sleep is a bad design. We have a similar question which talks about the comparison using Thread.sleep and Timer.
Try something like the following:
void Method()
{
console.log('statement 1');
console.log('statement 2');
var timer = new System.Threading.Timer(
o => // timer callback
{
console.log('statement 2');
},
15000, // Delay
0 // Repeat-interval; 0 for no repeat
);
}
Syntax is C# 3.0, uses a lambda expression to effectively create a closure around statement #3. With this, you could use any local variables of Method. A thing to note, however, is that with this method, or any other timer-based method...the function will return immediately after creating the timer. The function won't block until the Timer executes. To achieve that, the only thing I can think of is to actually use threads and make Method() block on a signal (i.e. WaitHandle, ResetEvent, etc.) until the timed call on the other thread completes.
Thread.sleep seems a sensible thing to do if there isn't anything else to do while waiting.
It puts the thread to sleep for that time so it doesn't use any CPU resources.
You could always use a timer and then execute code after the set duration. However, if you don't actually have to do anything and just want to wait at a particular point in code, then I think Thread.Sleep(150000); is sufficient.
[Edit: spelling]
If you always want to wait for a given time, then Sleep is useful. Obviously you shouldn't do this on a thread where timely responses are expected.
Keep in mind that your thread will sleep for the duration in all cases. If for some reason you want the thread to resume sooner, you're better off using signaling or callbacks. By using either of these instead of Sleep, you will minimize the needless wait time.
void Method()
{
Statement1();
Statement2();
// Start the timer for a single 15 second shot.
// Keep a reference to it (Mytimer) so that the timer doesn't get collected as garbage
Mytimer = new System.Threading.Timer((a) =>
{
// Invoke the 3rd statement on the GUI thread
BeginInvoke(new Action(()=>{ Statement3(); }));
},
null,
15000, // 15 seconds
System.Threading.Timeout.Infinite); // No repeat
}
I don't sure 100%, but if you really need your method to return after waiting 15 sec, try following:
Method()
{
stmt1();
stmt2();
int time = DateTime.Now.Millisecond;
while (15*1000 > DateTime.Now.Millisecond - time)
{
Thread.Sleep(10)
Application.DoEvents();
}
stmt3();
}

Sort of scheduler

I need to implement something. Something that could do some certain task in my program. For example every ten seconds, write something into a log in a file.
Of course it suppose to run in a background thread.
Where should I dig? I am not so familiar with multithreading. I've heard about BackgroundWorker class, but I'm not sure if it is appropriate here..
Use System.Threading.Timer, it will run a task in a ThreadPoool thread. That is the most efficient way for this.
Here is an example, every 10 seconds:
Timer aTimer = new System.Threading.Timer(MyTask, null, 0, 10000);
static void MyTask(object state)
{
...
}
Actually for WPF DispatcherTimer would be much better than the Async timer.
You could use the backgroundworker class for this, but it sounds like you just need to use Timer.

Categories