FacebookTwitter
Hatrack River Forum   
my profile login | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » C# help

   
Author Topic: C# help
mistaben
Member
Member # 8721

 - posted      Profile for mistaben           Edit/Delete Post 
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?

Posts: 105 | Registered: Oct 2005  |  IP: Logged | Report this post to a Moderator
Bokonon
Member
Member # 480

 - posted      Profile for Bokonon           Edit/Delete Post 
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

Posts: 7021 | Registered: Nov 1999  |  IP: Logged | Report this post to a Moderator
mistaben
Member
Member # 8721

 - posted      Profile for mistaben           Edit/Delete Post 
Thanks, Bok.

I haven't dealt with multiple threads before, but I think this may indeed be a simple (so to speak) solution.

Posts: 105 | Registered: Oct 2005  |  IP: Logged | Report this post to a Moderator
IanO
Member
Member # 186

 - posted      Profile for IanO   Email IanO         Edit/Delete Post 
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.

Posts: 1346 | Registered: Jun 1999  |  IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
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.

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Numinor West
Member
Member # 9375

 - posted      Profile for Numinor West   Email Numinor West         Edit/Delete Post 
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.

Posts: 90 | Registered: Apr 2006  |  IP: Logged | Report this post to a Moderator
Artemisia Tridentata
Member
Member # 8746

 - posted      Profile for Artemisia Tridentata   Email Artemisia Tridentata         Edit/Delete Post 
Leading tone in the key of D? Play it a little on the high side.
Posts: 1167 | Registered: Oct 2005  |  IP: Logged | Report this post to a Moderator
mistaben
Member
Member # 8721

 - posted      Profile for mistaben           Edit/Delete Post 
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]

Posts: 105 | Registered: Oct 2005  |  IP: Logged | Report this post to a Moderator
   

   Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

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