Click here to Skip to main content
15,884,353 members
Articles / Programming Languages / C#

Simple Real-Time Ticker Using C# / NET / WCF / WPF

Rate me:
Please Sign up or sign in to vote.
4.86/5 (23 votes)
1 Oct 2008CPOL2 min read 141K   4.6K   105  
This article is a basic working sample of using WCF and WPF to publish real-time data.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace WCFMarketDataServer
{
    //----------------------------------------------------------------------------------------------------------------
    /// <summary>
    /// This class contains parameters and otehr configuration information to 
    /// diagnose and set limits on internal state. 
    /// 
    /// This data should be read form the database, or config file. For this sample it is hard 
    /// coded.
    /// </summary>
    class CMarkerConfig
    {
        //------------------------------------------------------------
        public const int g_MaxCMDMsgQueueSz = 100;
        public const int g_MinCMDMsgQueuewaitMs = 5;

        //------------------------------------------------------------
        public const int g_MinLogMsgThresholdSec = 10;

        //------------------------------------------------------------
        static public bool g_bDiagnoseTick = true;


        //------------------------------------------------------------
        static public void LogError(string sMsg)
        {
            Log("ERROR: " + sMsg);
        }

        //------------------------------------------------------------
        static public void LogWarning(string sMsg)
        {
            Log("WARNING: " + sMsg);
        }

        //------------------------------------------------------------
        static public void LogInfo(string sMsg)
        {
            Log(sMsg);
        }

        //--------------------------------------------------------------------------------------------------------------------------------
        static public void LogExcptn(Exception ex)
        {
            Log(string.Format("ERROR: {0} \r\n {1}", ex.Message, ex.StackTrace));
        }

        //--------------------------------------------------------------------------------------------------------------------------------
        static private string Log(string sText)
        {
            //
            // Lock console stream since it can be called from multiple threads
            //
            lock (Console.Out)
            {
                Console.Write(string.Format("({1} - {0})  ", Thread.CurrentThread.ManagedThreadId.ToString("00000"), DateTime.Now.ToString("hh:mm:ss") ));
                Console.WriteLine(sText);

                //Debug.Write(Thread.CurrentThread.ManagedThreadId.ToString("00000  "));
                //Debug.WriteLine(sText);
            }
            return sText;
        }
    }



    //----------------------------------------------------------------------------------------------------------------
    /// <summary>
    /// This class controls the amount of message for the same type of error. This way we will not 
    /// flood the event log or log file with the same message.
    /// </summary>
    class CMsgTimer
    {
        private DateTime    m_DateTime = DateTime.Now;
        private int         m_SignalEverySec = 5;
        private bool        m_bReset = true;

        //------------------------------------------------------------
        public CMsgTimer(int iSignalEverySec)
        {
            m_SignalEverySec = iSignalEverySec;
        }

        //------------------------------------------------------------
        public void Reset()
        {
            m_DateTime = DateTime.Now;
            m_bReset = true;
        }

        //------------------------------------------------------------
        public bool Signal()
        {
            // 
            if (m_bReset)
            {
                m_DateTime = DateTime.Now;
                m_bReset = false;
                return true;
            }

            // Chech to see if it is time to signal
            if (m_SignalEverySec < DateTime.Now.Subtract(m_DateTime).TotalSeconds)
            {
                m_DateTime = DateTime.Now;
                return true;
            }

            return 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
Windows Consultant
United States United States
20+ yrs Leading and Developing Microsoft products in the Financial Industry.

My main background is VC++, server and client development.

Currently focused in WPF/XAML, Windows 8 and Windows Azure Server technologies.

Comments and Discussions