첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.00MB

▶ TimeCounter.cs

using System;
using System.Threading;

namespace TestProject
{
    /// <summary>
    /// 시간 계수기
    /// </summary>
    public class TimeCounter
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 인스턴스
        /// </summary>
        public static TimeCounter Instance => _instanceLazy.Value;

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 인스턴스 LAZY
        /// </summary>
        private static readonly Lazy<TimeCounter> _instanceLazy = new Lazy<TimeCounter>(() => new TimeCounter());

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Instance
        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 실행 여부
        /// </summary>
        private bool isRunning = false;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 현재 시간 - CurrentTime

        /// <summary>
        /// 현재 시간
        /// </summary>
        public DateTime CurrentTime { get; private set; } = DateTime.Now;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 생성자 - TimeCounter()

        /// <summary>
        /// 생성자
        /// </summary>
        private TimeCounter()
        {
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 시작하기 - Start()

        /// <summary>
        /// 시작하기
        /// </summary>
        public void Start()
        {
            this.isRunning = true;

            new Thread(new ThreadStart(ProcessThread)).Start();
        }

        #endregion
        #region 중단하기 - Stop()

        /// <summary>
        /// 중단하기
        /// </summary>
        public void Stop()
        {
            this.isRunning = false;
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 스레드 처리하기 - ProcessThread()

        /// <summary>
        /// 스레드 처리하기
        /// </summary>
        private void ProcessThread()
        {
            CurrentTime = DateTime.Now;

            while(this.isRunning)
            {
                CurrentTime = CurrentTime.AddSeconds(0.1);

                Thread.Sleep(100);
            }
        }

        #endregion
    }
}

 

728x90

 

▶ Program.cs

using System;
using System.Threading;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            TimeCounter.Instance.Start();

            for(int i = 0; i < 10; i++)
            {
                Console.WriteLine(TimeCounter.Instance.CurrentTime);

                Thread.Sleep(300);
            }

            TimeCounter.Instance.Stop();
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요