Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Easy Desktop Web Stats

, 23 Oct 2012
Create a Windows desktop app to display daily web stats from your personal site
using System;
using System.IO;
using System.Xml;
using System.Net;
using System.Windows.Forms;
using System.Media;

namespace Desktop_Web_Stats
{
    public partial class Main  
    {          
        // change this to the URL of your site
        private string statsURL = "http://www.megamillionswidget.com/currentstats.cshtml";

        private Timer dataRequestTimer = new Timer();
        private SoundPlayer ding = new SoundPlayer(Properties.Resources.Ding);        

        private DataGridViewCell messageCell;
        private DataGridViewCell tooltipCell;       
        
        private void CreateDataRequestCycle()
        {            
            dataRequestTimer.Tick += new EventHandler(dataRequestTimer_Tick);            
            statsGrid.CellClick += new DataGridViewCellEventHandler(statsGrid_CellClick);

            // initial delay of 8 seconds to allow Windows to finish loading other startup programs and to start the network connection;
            InitDataRequestCycle(8000);

            messageCell = statsGrid.Rows[0].Cells[0];
            messageCell.Value = "Wait...";

            tooltipCell = statsGrid.Rows[0].Cells[3];           
        }

        private void InitDataRequestCycle(int nInterval)
        {              
            dataRequestTimer.Interval = nInterval;
            dataRequestTimer.Start();            
        }
                
        private void dataRequestTimer_Tick(object sender, EventArgs e)
        {
            dataRequestTimer.Stop();             

            WebRequest statsRequest = null;              
            StreamReader statsStream;

            try
            {                
                string lastHitTimeString = (string)statsGrid.Rows[0].Cells[3].Value;

                statsRequest = WebRequest.Create(statsURL); 
                WebResponse statsResponse = statsRequest.GetResponse();
                statsStream = new StreamReader(statsResponse.GetResponseStream());
                XmlDocument statsXMLDocument = new XmlDocument();
                statsXMLDocument.Load(statsStream);
                XmlNode stats = statsXMLDocument.SelectSingleNode("Stats");

                short n = 0;
                foreach (XmlNode stat in stats)
                {
                    statsGrid.Rows[0].Cells[n++].Value = stat.InnerText;
                }               
                              
                tooltipCell.ToolTipText = "Last request: " + DateTime.Now.ToShortTimeString();
                
                if ((string)statsGrid.Rows[0].Cells[3].Value != lastHitTimeString && settings.playDing)
                {
                    ding.Play();
                }

                statsStream.Dispose();                
            }
            catch
            {                
                messageCell.Value = "Retry";
                messageCell.Selected = true;               
                
                statsRequest.Abort();
                
                InitDataRequestCycle(5000);
                return;
            }

            messageCell.Selected = false;
            InitDataRequestCycle(settings.updateInterval * 1000 * 60); 
        }      

        private void statsGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            statsGrid.CurrentCell.Selected = false;           
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Michael J Hill - OH

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 23 Oct 2012
Article Copyright 2012 by Michael J Hill - OH
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid