I have a lovely, working C# application. Now I need to add a function that runs in the background at a constant rate (eg 10 Hz), using one variable from the main part of the program and adjusting several others.
I can create a standalone program using a Timer (simply following the .NET documentation example) that works fine, but my attempts to use it in my app have been unsuccessful. That is, I can compile just fine, but then it doesn't do anything.
Any thoughts?
Posted by Bokonon (Member # 480) on :
You familiar with multithreading in C#? I'd guess that would be what you are looking for...
(I don't know multithreading specifically in C#, so I can't help you there)
-Bok
Posted by mistaben (Member # 8721) on :
Thanks, Bok.
I haven't dealt with multiple threads before, but I think this may indeed be a simple (so to speak) solution.
Posted by IanO (Member # 186) on :
Yeah, from your description, you want to run some sort of timer in a separate thread and then having it do something after a peried of sleep.
Most threading brings issues of concurrency into it- that is, shared variables/objects that one thread accesses (and changes) affecting other threads that are trying (or will try) to use that used/changed variable/object, leading to race conditions. This Article gives some information on interlocking threads and how to make them safer without causing race conditions or lockups.
Posted by fugu13 (Member # 2859) on :
You can still use one of the Timer classes, System.Threading.Timer .
Be warned, you can't be guaranteed an exact time between activation, only an approximation.
Posted by Numinor West (Member # 9375) on :
The Timer class does create its own thread. You should make a Service app with a Timer. Then, confirm that the timer works by doing a simple test in the timer event (10 sec interval) like write some text to a file. That way you know the timer works and can move on to your real logic.
Posted by Artemisia Tridentata (Member # 8746) on :
Leading tone in the key of D? Play it a little on the high side.
Posted by mistaben (Member # 8721) on :
Thanks all!
Numinor West,
That's what I was starting to figure out yesterday. However, as the paper concerning this project is due today, I'm going to try a different approach: performing my would-be-Timer task everytime a MIDI message comes in.