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

TestProject.zip
다운로드

▶ Program.cs

using System;

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

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

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            using(RepeatWorker worker = new RepeatWorker())
            {
                worker.SleepTime  = 1000;
                worker.WorkAction = p => DoWork1(p);

                worker.Start();

                Console.ReadKey(true);

                worker.SleepTime  = 100;
                worker.WorkAction = p => DoWork2(p);

                Console.ReadKey(true);

                worker.SleepTime  = 1000;
                worker.WorkAction = p => DoWork1(p);

                Console.ReadKey(true);
            }
        }

        #endregion

        #region 작업 1 실행하기 - DoWork1(parameter)

        /// <summary>
        /// 작업 1 실행하기
        /// </summary>
        /// <param name="parameter">매개 변수</param>
        private static void DoWork1(object parameter)
        {
            Console.WriteLine("작업 1 : {0}", DateTime.Now);
        }

        #endregion
        #region 작업 2 실행하기 - DoWork2(parameter)

        /// <summary>
        /// 작업 2 실행하기
        /// </summary>
        /// <param name="parameter">매개 변수</param>
        private static void DoWork2(object parameter)
        {
            Console.WriteLine("작업 2 : {0}", DateTime.Now);
        }

        #endregion
    }
}

 

728x90

 

▶ RepeatWorker.cs

using System;
using System.Threading;

namespace TestProject
{
    /// <summary>
    /// 반복 작업자
    /// </summary>
    public class RepeatWorker : IDisposable
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region Field

        /// <summary>
        /// 스레드
        /// </summary>
        protected Thread thread = null;

        /// <summary>
        /// 루프 계속 여부
        /// </summary>
        protected bool continueLoop = true;

        /// <summary>
        /// 휴지 여부
        /// </summary>
        protected bool isSleep = false;

        /// <summary>
        /// 휴지 시간 (단위 : 밀리초)
        /// </summary>
        protected int sleepTime = 1000;

        /// <summary>
        /// 작업 액션
        /// </summary>
        protected Action<object> workAction = null;

        /// <summary>
        /// 작업 매개 변수
        /// </summary>
        protected object workParameter = null;

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

        #endregion

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

        #region 스레드 - Thread

        /// <summary>
        /// 스레드
        /// </summary>
        public Thread Thread
        {
            get
            {
            return this.thread;
            }
        }
        
        #endregion
        #region 휴지 시간 (단위 : 밀리초) - SleepTime

        /// <summary>
        /// 휴지 시간 (단위 : 밀리초)
        /// </summary>
        public int SleepTime
        {
            get
            {
                return this.sleepTime;
            }
            set
            {
                this.sleepTime = value;
            }
        }

        #endregion
        #region 작업 액션 - WorkAction

        /// <summary>
        /// 작업 액션
        /// </summary>
        public Action<object> WorkAction
        {
            get
            {
                return this.workAction;
            }
            set
            {
                this.workAction = value;
            }
        }

        #endregion
        #region 작업 매개 변수 - WorkParameter

        /// <summary>
        /// 작업 매개 변수
        /// </summary>
        public object WorkParameter
        {
            get
            {
                return this.workParameter;
            }
            set
            {
                this.workParameter = value;
            }
        }

        #endregion
        #region 실행 여부 - IsRunning

        /// <summary>
        /// 실행 여부
        /// </summary>
        public bool IsRunning
        {
            get
            {
                return this.isRunning;
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - RepeatWorker()

        /// <summary>
        /// 생성자
        /// </summary>
        public RepeatWorker()
        {
        }

        #endregion

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

        #region 시작하기 - Start()

        /// <summary>
        /// 시작하기
        /// </summary>
        public void Start()
        {
            if(this.isRunning)
            {
                return;
            }

            this.isRunning = true;

            try
            {
                #region 스레드를 설정합니다.

                if(this.thread != null)
                {
                    if(this.thread.IsAlive)
                    {
                        this.thread.Abort();
                    }

                    this.thread = null;
                }

                this.thread = new Thread(new ThreadStart(ProcessThread));

                this.thread.IsBackground = true;

                #endregion

                this.thread.Start();
            }
            catch(Exception exception)
            {
                this.isRunning = false;

                throw exception;
            }
        }

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

        /// <summary>
        /// 중단하기
        /// </summary>
        public void Stop()
        {
            if(!this.isRunning)
            {
                return;
            }

            this.isRunning = false;

            #region 스레드를 중단합니다.

            this.continueLoop = false;

            Thread.Sleep(500);

            if(this.thread != null && this.thread.IsAlive)
            {
                if(this.isSleep)
                {
                    this.thread.Abort();
                }
            }

            #endregion
        }

        #endregion

        #region 리소스 해제하기 - Dispose()

        /// <summary>
        /// 리소스 해제하기
        /// </summary>
        public void Dispose()
        {
            Stop();
        }

        #endregion

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

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

        /// <summary>
        /// 스레드 처리하기
        /// </summary>
        private void ProcessThread()
        {
            while(this.continueLoop)
            {
                this.workAction?.Invoke(this.workParameter);

                if(!this.continueLoop)
                {
                    break;
                }

                this.isSleep = true;

                Thread.Sleep(this.sleepTime);

                this.isSleep = false;
            }
        }

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

댓글을 달아 주세요