• Advertisement
Sign in to follow this  

[.net] C# Timers/multithreading question

This topic is 4493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Ummm... it doesn't have an Enabled property:

'System.Threading.Timer' does not contain a definition for 'Enabled'

But thanks anyways!

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
Note that right now this is all in the same class, Main method. Just trying to figure out how to get this all working.

Thanks!

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
Sign in to follow this  

  • Advertisement