Click here to Skip to main content
14,977,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help in the below code example that i got from a book.In the example iam trying to call a methode TimedMethod() after ten minutes repeatedly after a secod using the Timer class of System.Threadind name space. I was told to use the Start() method(commented in the following code) of the Timer class but i get an error indicating that the class has no such method. How can i achieve this or what can be the problem here?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ImplementingTimers
{
    class Program
    {
        static void TimedMethod(Object stateInfo)
        {
            Console.WriteLine("Executed");
        }

        static void Main(string[] args)
        {
            TimerCallback timerDelegate = new TimerCallback(TimedMethod);
            Timer stateTimer = new Timer(timerDelegate, null, 10000, 1000);
            //stateTimer.Start();
            
        }
    }
}
Posted
Updated 2-Jun-12 0:05am
v2
Comments
hitech_s 2-Jun-12 6:05am
   
<pre> tags added....

1 solution

That is not how the System.Threading.Timer[^] works...

"When you create a timer, you can specify an amount of time to wait before the first execution of the method (due time), and an amount of time to wait between subsequent executions (period). You can change these values, or disable the timer, using the Change method." -MSDN.

Are you sure the author of the book is not using one of the other two Timers[^]? A System.Timers.Timer[^] or System.Windows.Forms.Timer[^]?
These two DO have a Start Method (although they don't have a constructor that takes a delegate as parameter)... :)
Hope that helps.
   
Comments
thusolebalang 2-Jun-12 8:14am
   
Thanks for that Naerling. I also found out that the Timer class used have no such method but i was wondering why it is used in the book. I am very sure that the class used here is a "System.Threading.Timer" class because its under the topic "Multithreaded Applications" in the book. I am getting to think that the author made a mistake because the author also described the (Start(), Dispose() and Close()) methods as the only methods of this class. But is there a way of achieving task of this example using this class. Please if possible edit my code and show me.
Sander Rossel 2-Jun-12 9:42am
   
The Threading.Timer starts automatically. In your code it should print "Executed" after 10 seconds and then after every second after that. For more info I must point you toward the MSDN article, because it has a full sample and explanation.
Please don't forget to vote and accept the answer if it helped you :)
Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900