Click here to Skip to main content
15,891,204 members
Articles / Desktop Programming / Win32

Simple Network Status Monitor Example

Rate me:
Please Sign up or sign in to vote.
4.90/5 (16 votes)
5 Jun 2011CPOL2 min read 61.9K   2.8K   53  
Shows how to implement a system to generate events when network interfaces change.
/*******************************************************************************
 *******************************************************************************
            Author: Simon Bridge, June 2011 mailto:srbridge@gmail.com
 
 
        This code is provided under the Code Project Open Licence (CPOL)
          See http://www.codeproject.com/info/cpol10.aspx for details
  
 *******************************************************************************
 ******************************************************************************/

using System;
using System.Diagnostics;
using System.Threading;

namespace NetworkStatus
{
    /// <summary>
    /// central point through which to write console messages. options to easily set the color of the text.
    /// </summary>
    public static class SysConsole
    {
        private static int threadNo = 0;

        /// <summary>
        /// if the calling thread has no name specified, assign it one:
        /// </summary>
        [DebuggerStepThrough]
        private static void threadName()
        {
            if (Thread.CurrentThread.Name == null)
                Thread.CurrentThread.Name = "Anon" + (++threadNo).ToString();
        }

        /// <summary>
        /// write a line.
        /// </summary>
        [DebuggerStepThrough]
        public static void WriteLine()
        {
            Console.WriteLine();
        }

        /// <summary>
        /// write the specified text. will include the thread name and time.
        /// </summary>
        /// <param name="text"></param>
        [DebuggerStepThrough]
        public static void Write(String text)
        {
            WriteLineHeader();
            Console.Write(text);
        }

        /// <summary>
        /// write the specified line text.
        /// </summary>
        /// <param name="text"></param>
        [DebuggerStepThrough]
        public static void WriteLine(String text, ConsoleColor color)
        {
            WriteLineHeader();
            ConsoleColor cc = Console.ForegroundColor;
            Console.ForegroundColor = color;
            Console.WriteLine(text);
            Console.ForegroundColor = cc;
        }

        /// <summary>
        /// write the specified line text.
        /// </summary>
        /// <param name="text"></param>
        [DebuggerStepThrough]
        public static void WriteLine(String text)
        {
            WriteLineHeader();
            Console.WriteLine(text);
        }

        /// <summary>
        /// write the specified text-line.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="fmt"></param>
        public static void WriteLine(String text, params Object[] fmt)
        {
            WriteLineHeader();
            Console.WriteLine(text, fmt);
        }

        /// <summary>
        /// append the header to the line about to be written: ThreadName: HH:mm:ss
        /// </summary>
        [DebuggerStepThrough]
        private static void WriteLineHeader()
        {
            threadName();
            ConsoleColor cc = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write(Thread.CurrentThread.Name);
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write(": ");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write(DateTime.Now.ToString("HH:mm:ss") + " ");
            Console.ForegroundColor = cc;
        }

        /// <summary>
        /// wait for the enter key to be pressed.
        /// </summary>
        [DebuggerStepThrough]
        public static void WaitEnterKey()
        {
            Console.ReadLine();
        }

        /// <summary>
        /// wait for any key to be pressed.
        /// </summary>
        [DebuggerStepThrough]
        public static void WaitAnyKey()
        {
            Console.ReadKey(true);
        }
    }
}

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
Software Developer (Senior) Decipha
Australia Australia
Wrote his first computer game in Microsoft Basic, on a Dragon 32 at age 7. It wasn't very good.
Has been working as a consultant and developer for the last 15 years,
Discovered C# shortly after it was created, and hasn't looked back.
Feels weird talking about himself in the third person.

Comments and Discussions