[.net] C# Timers/multithreading question

Started by
6 comments, last by xycos 18 years, 5 months ago
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
"For sweetest things turn sour'st by their deeds;Lilies that fester smell far worse than weeds."- William Shakespere, Sonnet 94
Advertisement
you need to enable the timer

Timer t = new Timer(blah blah blah);
t.Enabled = true;

that's how you start a timer.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Ummm... it doesn't have an Enabled property:

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

But thanks anyways!
"For sweetest things turn sour'st by their deeds;Lilies that fester smell far worse than weeds."- William Shakespere, Sonnet 94
oh, apparently there are two Timer classes. I usually use System.Timers.Timer.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

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]

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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.
"For sweetest things turn sour'st by their deeds;Lilies that fester smell far worse than weeds."- William Shakespere, Sonnet 94
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!
"For sweetest things turn sour'st by their deeds;Lilies that fester smell far worse than weeds."- William Shakespere, Sonnet 94
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!
"For sweetest things turn sour'st by their deeds;Lilies that fester smell far worse than weeds."- William Shakespere, Sonnet 94

This topic is closed to new replies.

Advertisement