Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Timeout the execution of a function


  • Please log in to reply
3 replies to this topic

#1 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 18 April 2012 - 04:50 PM

I have a function which returns a HTML page, which every so often gets hung up. It works the vast majority of the time, but every so often the server it is trying to talk with doesn't respond back. Is there a way to setup a timeout on the function? Thanks
Steve


My Other Hobby: Engineer Turned Baker

#2 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 18 April 2012 - 07:08 PM

I think I figured this out. Ill post my code after I have thoroughly tested it.
Steve


My Other Hobby: Engineer Turned Baker

#3 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 18 April 2012 - 07:50 PM

Here is a preliminary code that has only been tested on the emulator since I don't have my netduino with me right now.

Here is the helper class
using System;
using System.Threading;
using System.Runtime.Remoting;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.IO;
using System.Text;
using Toolbox.NETMF.NET;
using System.Diagnostics;

namespace EmailStockChecker
{
    public class clWebpageHelper
    {

        private static string _rawData = "";
        private static string _url = "";
        private static string _pageLocation = "";

        //ALLOWS FOR A MAXIMUM OF 10 SECONDS FOR THE THREAD TO FINISH BEFORE IT IS KILLED.
        private static int _maxTimeOut = 10 * 1000;//5 * 1000;

        private static Thread newThread = new Thread(new ThreadStart(Work));

        //public static void SetWebPage(string url, string pageLocation)
        //{
        //    _url = url;
        //    _pageLocation = pageLocation;
        //    _rawData = "";

        //    //newThread = new Thread(new ThreadStart(Work));
        //}

        public static void SetMaxTimeOut(int microseconds)
        {
            _maxTimeOut = microseconds;
        }
        public static string GetRawData(string url, string pageLocation)
        {
            _url = url;
            _pageLocation = pageLocation;
            _rawData = "";
            
            newThread = new Thread(new ThreadStart(Work));
            newThread.Start();

            if (newThread.Join(_maxTimeOut + _maxTimeOut))
            {
                //Debug.Print("UPDATE: JOINED");
            }
            else
            {
                Debug.Print("ERROR: TIMED OUT");
                newThread.Suspend();
                newThread = new Thread(new ThreadStart(Work));
                //newThread.Abort();
            }
            return _rawData;
        }

        private static void Work()
        {
            _rawData = GetOnlineWebPage(_url, _pageLocation);
            //Debug.Print("UPDATE: WORK THREAD COMPLETE");
        }
        
        public static string GetOnlineWebPage(string url, string pageLocation)
        {
            /* THIS FUNCTION GETS ONLINEWEBPAGES AS LONG AS THEY ARE NOT HTTPS. THE NETDUINO AT THIS POINT DOESNT
             * HANDLE DEALING WITH HTTPS PAGES OR SSL. THE WORK AROUND TO THIS IS TO USE AN PHP WEBSITE. THE FUNCITON 
             * RETURNS THE ENTIRE WEBSITE AS A LONG, LONG STRING. ALL OF THE HTML WILL BE RETURNED WITH THIS FUNCTION
             * SUCH AS HEADER AND BODY BREAKS. THIS FUNCTION GOES HAND IN HAND WITH GETSTRINGINBETWEEN
             * 
             * INPUT:   URL     --->    A STRING WHICH CONTAINS THE URL OF THE SITE SUCH AS HTTP://WWW.GMAIL.COM
             * 
             * OUTPUT:  A STRING WHICH CONTAINS ALL THE RAW DATA OF THE WEBSITE
             * 
             * SAMPLE CALL:         clMisc.getOnlineWebPage("www.smarcus3.x10.mx");
             * 
             * BASED ON THE INTEGRATEDSOCKET EXAMPLE
             */
            //string Text = "";
            try
            {
                Debug.Print("WEBPAGE: " + url + pageLocation + " ATTEMPTING TO GET");

                StringBuilder sb = new StringBuilder();

                // Defines the socket, including the remote host and port
                SimpleSocket Socket = new IntegratedSocket(url, 80); //SimpleSocket Socket = new IntegratedSocket("www.smarcus3.x10.mx", 80);

                // Connects to the socket
                Socket.Connect();

                // /ig/api?stock=EEM for finance
                // / for the root file i.e. emails

                // Does a plain HTTP request
                Socket.Send("GET " + pageLocation + " HTTP/1.1\r\n");
                Socket.Send("Host: " + Socket.Hostname + "\r\n");
                Socket.Send("Connection: Close\r\n");
                Socket.Send("\r\n");


                // Prints all received data to the debug window, until the connection is terminated and there's no data left anymore
               
                while (Socket.IsConnected || Socket.BytesAvailable > 0)
                {
                    sb.Append(Socket.Receive());
                }

                // Closes down the socket
                Socket.Close();
                Debug.Print("WEBPAGE: " + url + pageLocation + " GRABBED");
                return sb.ToString();
            }
            catch
            {
                Debug.Print("ERROR: FAILED TO START TO GRAB WEB PAGE: " + url + pageLocation);
                return "";
            }
        }
    }
}

Here is how you call it in a function such as in the main function

clWebpageHelper.GetRawData("www.google.com", "/")

Steve


My Other Hobby: Engineer Turned Baker

#4 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 18 April 2012 - 10:34 PM

I am testing it now with a 2500 ms maxTimeOut and sometimes my personal website doens't load in time (i.e. times out), but the code takes care of it. Hopefully this new addition will make sure an HTTP hangup will not stop the controller's main loop for longer than the maxTimeOut is set for.
Steve


My Other Hobby: Engineer Turned Baker




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.