This is topic C# help in forum Books, Films, Food and Culture at Hatrack River Forum.


To visit this topic, use this URL:
http://www.hatrack.com/ubb/main/ultimatebb.php?ubb=get_topic;f=2;t=043184

Posted by mistaben (Member # 8721) on :
 
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.

Here's a good list of articles: http://www.c-sharpcorner.com/Multithreading.asp
especially this introduction: http://www.c-sharpcorner.com/Code/2005/April/Thread.asp

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 .

This article might be of some help: http://www.dotnet247.com/247reference/a.aspx?u=http://www.c-sharpcorner.com/Code/2002/April/MtP4MtVsMt.asp

Further information of possible use (behaves the same as .Net's Timer): http://www.dotgnu.org/pnetlib-doc/System/Threading/Timer.html

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.

AT,

I was wondering how long it would take... [Smile]
 


Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2