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

TestProject.zip
0.00MB

▶ TimeSleepManager.cs

using System;
using System.Threading;

namespace TestProject
{
    /// <summary>
    /// 시간 휴지 관리자
    /// </summary>
    public static class TimeSleepManager
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 마지막 틱 카운트
        /// </summary>
        public static long LastTickCount;

        #endregion

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

        #region Field

        /// <summary>
        /// 전체 시간 1
        /// </summary>
        private static int _totalTime1 = 0;

        /// <summary>
        /// 카운트 1
        /// </summary>
        private static int _count1;

        /// <summary>
        /// 전체 시간 2
        /// </summary>
        private static int _totalTime2 = 0;

        /// <summary>
        /// 카운트 2
        /// </summary>
        private static int _count2;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 시작하기 - Start()

        /// <summary>
        /// 시작하기
        /// </summary>
        public static void Start()
        {
            LastTickCount = DateTime.Now.Ticks;
        }

        #endregion
        #region 휴지하기 - Sleep()

        /// <summary>
        /// 휴지하기
        /// </summary>
        public static void Sleep()
        {
            long currentTickCount = DateTime.Now.Ticks;

            int deltaTime = (int)((currentTickCount - LastTickCount) / 10000d);

            Console.WriteLine($"작업 시간 : {deltaTime}");

            LastTickCount = currentTickCount;

            _count1++;

            _totalTime1 += deltaTime;

            int sleep1;

            if(_count1 == 5) // 5번
            {
                if(_totalTime1 >= 1000) // 1초
                {
                    sleep1 = 0;
                }
                else
                {
                    sleep1 = 1000 - _totalTime1;
                }

                _count1     = 0;
                _totalTime1 = 0;
            }
            else
            {
                sleep1 = 0;
            }

            _count2++;

            _totalTime2 += deltaTime;

            int sleep2;

            if(_count2 == 1000) // 1,000번
            {
                if(_totalTime2 >= 3600000) // 60분
                {
                    sleep2 = 0;
                }
                else
                {
                    sleep2 = 3600000 - _totalTime2;
                }

                _count2     = 0;
                _totalTime2 = 0;
            }
            else
            {
                sleep2 = 0;
            }


            int finalSleep = sleep1 > sleep2 ? sleep1 : sleep2;

            Console.WriteLine($"휴지 시간 : {finalSleep}");

            if(finalSleep > 0)
            {
                Thread.Sleep(finalSleep);

                LastTickCount = DateTime.Now.Ticks;
            }
        }

        #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()
        {
            Random random = new Random(DateTime.Now.Millisecond);

            TimeSleepManager.Start();

            for(int i = 0; i < 100; i++)
            {
                Thread.Sleep(random.Next(100, 300)); // 작업을 가정한다.

                TimeSleepManager.Sleep();
            }
        }

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

댓글을 달아 주세요