Sign in to follow this  
xycos

[.net] C# Timers/multithreading question

Recommended Posts

Okay, so... I've only worked with Java so far, and never even understood multithreading in that, but anyway, now I'm REALLY lost. Yes, I looked all over for tutorials, but they were all talking about applications in windows forms which have their own threading thing going on apparently, and mine is a console application. So here's my question. I have this (using Syste.Threading.Timer):
t = new Timer(callback, null, Timeout.Infinite, Timeout.Infinite);
t.Change(0, Settings.FRAMERATE); //Start the timer
Now what do I do? If I just say
while(true) { }
nothing happens. If I say
Thread.Sleep(500);
this thread sleeps for a bit, then executes again, but the timer doesn't actually work. Do I need to put the timer in a sperate object or something? Is Sytem.Threading.Timer the wrong one/should I use System.Timing.Timer? Thanks and best of wishes, xycos

Share this post


Link to post
Share on other sites
What do you mean it doesn't actually work? It's not firing? or what?

Also, there are three seperate timers in the framework, each applicable to it's own particular usage patterns. There's System.Timers.Timer, System.Threading.Timer and System.Windows.Forms.Timer [grin]

Share this post


Link to post
Share on other sites
Okay, I tried System.Timers.Timer, it still does the same thing.

t = new Timer(Settings.FRAMERATE);
t.Elapsed += new ElapsedEventHandler(tick);
t.Enabled = true;


And, yes, I mean it's not firing. I've tested this with console output, etc.

Share this post


Link to post
Share on other sites
Oops, I lie!

Using System.Timers.Timer I can get it to fire using System.Threading.Thread.Sleep(). But I don't want the thread (that includes my graphics engine, whatever) to sleep. Is there a way to make it check if the timer has fired, and if it hasn't, just to keep on doing what it's doing? Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this