Problem with timer on Spider

Hello,

I have a small problem with a timer, because I want to send information (ReqGet()) every 50 seconds, but the function "timer_Tick " doesn’t work.

As soon as you have an idea ?


namespace Wifi
{
    public partial class Program
    {
        void ProgramStarted()
        {
            Debug.Print("Program Started");
            GT.Timer timer = new GT.Timer(30000, GT.Timer.BehaviorType.RunContinuously);
            timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);

            if (Startwifi())
            {
                if (testReq())
                {
                    timer.Start();
                }
            }
            //Thread.Sleep(Timeout.Infinite);
        }

        private void timer_Tick(GT.Timer timer)
        {
            ReqGet();
        }

        bool testReq()
        {
            HttpWebResponse response;
            HttpWebRequest request;
            Debug.Print("Generating Request");
            request = (HttpWebRequest)HttpWebRequest.Create("http://google.ch");
            request.Method = "GET";
            request.UserAgent = "MyApp NETMF";
            request.KeepAlive = false;
            request.Timeout = 1000;

            Debug.Print("Awaiting response...");
            response = (HttpWebResponse)request.GetResponse();
            Debug.Print("Done.\n" + response.ContentLength.ToString() + " bytes");
            if (response.ContentLength > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        bool Startwifi()
        {
            if (wifi_RS21 != null)
            {
                if (wifi_RS21.Interface.IsOpen)
                {
                    wifi_RS21.Interface.Disconnect();
                }
            }

            string SSID = "SGC";
            string password = "TechCFPT_2012";
            bool conn = false;

            wifi_RS21.Interface.Open();
            NetworkInterfaceExtension.AssignNetworkingStackTo(wifi_RS21.Interface);
            WiFiNetworkInfo[] infos = wifi_RS21.Interface.Scan();
            foreach (WiFiNetworkInfo info in infos)
            {
                if (info.SSID == SSID)
                {
                    wifi_RS21.Interface.NetworkInterface.EnableDhcp();
                    wifi_RS21.Interface.Join(info, password);
                    Debug.Print(SSID + " Connected");
                    conn = true;
                    break;
                }
                else
                {
                    conn = false;
                    Debug.Print(SSID + " not found");
                }
            }
            return conn;
        }

        void ReqGet()
        {
            string finalUri = "http://albentrix.com/fez.php?value=20";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(finalUri);
            request.Method = "GET";
            request.UserAgent = "MyApp NETMF";
            request.KeepAlive = false;
            request.Timeout = 1000;

            WebResponse resp = null;
            try
            {
                resp = request.GetResponse();
            }
            catch (Exception e)
            {
                Debug.Print("Exception in HttpWebRequest.GetResponse(): " + e.ToString());
            }

            if (resp != null)
            {
                Debug.Print("ReqOK");
            }
        }
    }
}

You timer is local to ProgramStarted. As soon as it is done it is due for garbage collection and eventually is disposed. Make it a class member (Declare it a the class scope).

GT.Timer timer;

void ProgramStarted()
        {
            Debug.Print("Program Started");
            timer = new GT.Timer(30000, GT.Timer.BehaviorType.RunContinuously);
            timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
 
            if (Startwifi())
            {
                if (testReq())
                {
                    timer.Start();
                }
            }
            //Thread.Sleep(Timeout.Infinite);
        }

By the way your timer is set for every 30 seconds not 50 as you have mentioned.

1 Like

Indeed, this was it. Thank you very much.

The timer value was indicative. :slight_smile:

You are welcome!