Using Timers in a console application

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


Answer this question

Using Timers in a console application

  • sjhuk

    Thanks, but now I'm even more confused.

    When I try to do anything(i.e. write to the console just to check if it's working or whatever) I get an exception. The console-writing one was an IOException

    ("The handle is invalid.") on Console.WriteLine("test");

    Thanks and best of wishes,
    xycos

  • sv_canada

    Hi,

    If you're trying to create a multi-threaded application then you'll have to use a Thread object. A timer object executes a different thread on a given time. Try this:

            static void Main(string[] args) {
                Thread t = new Thread(new ThreadStart(Program.test));

                t.Start();
            }

            static void test() {
                Console.WriteLine("TEsting");
               
            }


    cheers,

    Paul June A. Domag


  • SNMSDN

    Hi,


    I've tried the code above using VS2005. Could you post your sample code on how your doing threading This would definitely speeden up the process of finding the solution to your problem...





    cheers,

    Paul June A. Domag

  • Sachin Choudhary

    You may wish to have a look at my article on threading which covers timers (of various different kinds).

    Other than that, as Paul said, show us the code - preferably as a short but complete example.

    Jon



  • Using Timers in a console application