Main Site Documentation

Timer issue


#1

Cannot get the Timer to work in the current context. I copied an example and changed the timer callback to my method and get Error 1 “No overload for ‘UpdateTime’ matches delegate ‘System.Threading.TimerCallback’”. I am sure that the error is on me but I just don’t understand the language enough to know what it is talking about. Any help?


public static void Main()
            {
                               
                const Int32 c_port = 80;                            // Port used for http requests
                byte[] ip = { 192, 168, 0, 150 };                   // Assigned IP address
                byte[] subnet = { 255, 255, 255, 0 };               // Assigned Subnet
                byte[] gateway = { 192, 168, 0, 1 };                // Assigned Gateway Address
                byte[] mac = { 0x00, 0x9A, 0xD2, 0xC2, 0xD9, 0x2A }; //Randomly generated MAC from http://www.macvendorlookup.com
                WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di9, true); // WIZnet interface with FEZ Connect
                //NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac); //Assigns ip/subnet/gateway/mac to Wiznet
                //NetworkInterface.EnableStaticDns(new byte[] { 192, 168, 0, 1 }); //Sets Static DNS address
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, c_port);

                try
                {
                    Dhcp.EnableDhcp(mac); //Used to check if there were any problems with the network settings (And at work)
                }
                catch
                {
                    Debug.Print("Connection failed. Verify cable is connected and network is operational.");
                }
                Debug.Print("Current Time: " + DateTime.Now.ToString()); //displays current time in VS2010 output window
                Internet updateTime = new Internet(); // Creates an instance of the class 'myTime' so it can be called for updating the RTC
                updateTime.NTPTime("time-a.nist.gov", -300); // Calls function to Update RTC from an NTP server "time-a.nist.gov" and offsets time for CST
                Debug.Print("Updated Time: " + DateTime.Now.ToString()); //displays updated time in VS2010 output window
                //ERROR IS ON THIS TIMER OBJECT///////////////////////////////////////////////////                                          
                Timer Updatetimer = new Timer(new TimerCallback(UpdateTime), null, 1000, 2000); //
                //////////////////////////////////////////////////////////////////////////////////
                Debug.GC(true); // Enable Garbage Collector
                Debug.EnableGCMessages(true); // Enable / Disable Garbage Collector Messages

                
                Thread.Sleep(Timeout.Infinite);                            
            }

            static void UpdateTime()
            {
                    Dhcp.ReleaseDhcpLease();
                    Dhcp.RenewDhcpLease();
                    Internet update = new Internet();
                    update.NTPTime("time-a.nist.gov", -300);
                    Debug.Print("Network now connected and time updated to:" +  DateTime.Now.ToString());
                
            }


#2

Would this help http://www.tinyclr.com/forum/2/3080/

Looks like you already replied there so you know about it!


#3

The problem I am having is with the timer object that calls the UpdateTime function. The NTP time issue is solved thanks to Nick. What I am trying to do is update the RTC clock every 60sec but the code returns an error during compile.


#4

Have you looked at our “super support page” http://www.tinyclr.com/support/ :slight_smile:

Look for “timer” there


#5

it has to be:


            static void UpdateTime(object o) // <<<---- here was your mistake
            {
                    Dhcp.ReleaseDhcpLease();
                    Dhcp.RenewDhcpLease();
                    Internet update = new Internet();
                    update.NTPTime("time-a.nist.gov", -300);
                    Debug.Print("Network now connected and time updated to:" +  DateTime.Now.ToString());
 
            }


#6

What is “object o” and why does it need to be passed? What does it contain?


#7

I got the example # http://wiki.tinyclr.com/index.php?title=SystemTimers to work with no problems but I still get errors when using the timer to update the RTC(See picture). What am I missing?


#8

You forgot to copy ‘new’ before TimerCallback


#9

Thanks!!!


#10

You’re welcome.

BTW: The o object will contain the state object you pass to the Timer constructor, in your case it is null.