Microsoft MVP성태의 닷넷 이야기
글쓴 사람
spowner (spowner at naver.com)
홈페이지
첨부 파일
 
부모글 보이기/감추기

다음 소스는 Stopwatch를 이용하여 마이크로세컨드 단위로 대기 하는 클래스를 만들어 보았습니다. (물론, CPU 점유 버젼입니다)
다른 분들께 참고하시라고 공유 드립니다.

사용법은 다음과 같습니다.

var w = Wait.Start(1000000); // 1000000 마이크로초(1초) 대기

// .. 다른 코드 : 다른 코드가 실행하면서 발생하는 지연도 반영하기 위함

w.Sleep();

---------------------------------------------------------------------------------------------------

    public class Wait
    {
        private static bool isHighResolution;
        private static long frequency;
        private ManualResetEvent re;
        private Stopwatch sw;
        private long ticks;
        private long finalTicks;


        static Wait()
        {
            isHighResolution = Stopwatch.IsHighResolution;
            frequency = Stopwatch.Frequency;
        }

        protected Wait(long microseconds)
        {
            re = new ManualResetEvent(false);

            sw = Stopwatch.StartNew();

            if (isHighResolution == false)
                throw new NotSupportHighResolutionException("System is not support high resolution frequency.");

            ticks = microseconds * frequency / 1000000;
        }

        public static Wait Start(long microseconds)
        {
            Wait result = new Wait(microseconds);
            return result;
        }

        public void Sleep()
        {
            while (sw.ElapsedTicks <= ticks)
                ;// Thread.SpinWait(100);
            //Thread.Sleep(1);
            //re.WaitOne(1);

            finalTicks = sw.ElapsedTicks;
        }

        public long TotalMicroseconds
        {
            get
            {
                return finalTicks * 1000000 / frequency;
            }
        }
    }

    public class NotSupportHighResolutionException : Exception
    {
        public NotSupportHighResolutionException(string msg)
            : base(msg)
        {
        }
    }








[최초 등록일: ]
[최종 수정일: 4/18/2015]


비밀번호

댓글 작성자
 



2015-05-20 02시50분
[이성환] 아.. 윗 글에 댓글을 달았는데 답글이 있었군요.
저랑 비슷하게 구현하셨네요. 역시 SpinWait밖에 없는 건가...;ㅅ;
[guest]

... 76  77  78  79  80  81  82  83  84  85  86  87  88  89  [90]  ...
NoWriterDateCnt.TitleFile(s)
180최성우5/7/20056880[Q] POST 형식의 데이터 훅킹?
182정성태5/9/20057437    답변글 [답변]: [Q] POST 형식의 데이터 훅킹?
190최성우5/10/20056452        답변글 [답변]: [답변]: [Q] POST 형식의 데이터 훅킹?
177최정희5/4/20056868네트워크 케이블의 연결상태
178정성태5/4/20056933    답변글 [답변]: 네트워크 케이블의 연결상태 [1]
188최정희5/10/20056495        답변글 [답변]: [답변]: 네트워크 케이블의 연결상태
189정성태5/10/20056777            답변글 [답변]: [답변]: [답변]: 네트워크 케이블의 연결상태 [2]
191최정희5/11/20056410                답변글 [답변]: [답변]: [답변]: [답변]: 네트워크 케이블의 연결상태 [1]
175안연준5/3/20056387IE 제어에 대한 궁금 증 ㅡ,.ㅡ;;
179정성태5/4/20057236    답변글 [답변]: IE 제어에 대한 궁금 증 ㅡ,.ㅡ;;
168안연준5/2/20056666[Database] Connection Error파일 다운로드1
169정성태5/2/20056910    답변글 [답변]: [Database] Connection Error
170안연준5/2/20056527        답변글 [답변]: [답변]: 계속 에러가 똑같애요
171정성태5/2/20057223            답변글 [답변]: [답변]: [답변]: 계속 에러가 똑같애요
172안연준5/2/20056962                답변글 [답변]: [답변]: [답변]: [답변]: 계속 에러가 똑같애요파일 다운로드1
173정성태5/2/20056771                    답변글 [답변]: [답변]: [답변]: [답변]: [답변]: 계속 에러가 똑같애요
174안연준5/3/20056641                        답변글 [답변]: [답변]: [답변]: [답변]: [답변]: [답변]: 계속 에러가 똑같애요
165장희석4/22/20057225[질문]ASP에서 ATL 서버 컴퍼넌트로 바이너리 데이타 전달하기
167정성태4/29/20057060    답변글 [답변]: [질문]ASP에서 ATL 서버 컴퍼넌트로 바이너리 데이타 전달하기
160카심4/21/20056805Internet Explorer 에서의 닷넷 Smart Client 개발
163정성태4/22/20056826    답변글 [답변]: Internet Explorer 에서의 닷넷 Smart Client 개발
159신대철4/21/20056484자동 로긴 프로그램
162정성태4/22/20056492    답변글 [답변]: 자동 로긴 프로그램
166신대철4/22/20056280        답변글 [답변]: [답변]: 자동 로긴 프로그램파일 다운로드1
157이용휘4/20/20056230w3ip를 통해서 윈도우 미디어 화을을 올려놓을 서버..
158정성태4/20/20056675    답변글 [답변]: w3ip를 통해서 윈도우 미디어 화을을 올려놓을 서버..
... 76  77  78  79  80  81  82  83  84  85  86  87  88  89  [90]  ...