Click here to Skip to main content
15,881,840 members
Articles / Programming Languages / C# 4.0

Easy Desktop Web Stats

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
23 Oct 2012CPOL4 min read 11.6K   120   5  
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions