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]

... 61  62  63  [64]  65  66  67  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
976박성준6/13/201113004VS2008 Add-in 구현 관련 질문 [4]
972김길6/6/201115040메모리 해제 예외 처리 관련.. [2]
971강동원5/29/201112741firebird install건 [1]
970임동찬5/18/201113468ASP.net 솔루션 디버깅 관련 [1]
969이성환5/4/201115380WMI 를 사용하지 않고 하드웨어 정보를 가져올 수 없을까요? [3]
968김동미4/28/201114649안녕하세요 다시 한번 문의를 드립니다.. [2]파일 다운로드1
967임동찬4/22/201117097C# using문 관련 [9]
964김동미4/18/201114850wcf IsOneWay 속성관련 문의 입니다..
965정성태4/18/201115910    답변글 [답변]: wcf IsOneWay 속성관련 문의 입니다..
966김동미4/19/201114216        답변글 [답변]: [답변]: wcf IsOneWay 속성관련 문의 입니다.. [1]
963최재훈4/12/201112487wcf inactivityTimeout 설정시 문의 사항이 있습니다. [2]
962임동찬4/8/201112219TFS 사용관련 [1]
961임동찬4/7/201112558XSD & XML & XmlCodeGenerator [2]
960임동찬4/5/201113811XML Schema Editor [4]
959immm3/24/201111844로그인 연동 어려운 건가요? [1]
958꼭지3/3/201113508Supporting compressed request in WCF 3.5 [5]
957임동찬2/21/201113919WCF channel faulted 관련 [2]
956윤용한2/18/201117287WaitHandle.WaitOne 과 Stopwatch에 관한 질문 [3]
955최광욱2/17/201114471TFS 에서 소스 영구 제거 방법 [1]
954한장우2/16/201112098atl activeX 질문이요~ [1]
952박용운2/16/201113029IE8.0에서 BHO [1]
953박용운2/16/201113147    답변글 [답변]: IE8.0에서 BHO
951임동찬2/11/201112996WCF Service Reference [1]
950이성환2/9/201114281Windows application 프로젝트를 참조 했을 때 생성되는 실행파일을 직접 실행 불가능하도록 하고 싶습니다. [6]파일 다운로드1
947김순조1/24/201114301.NET based Com에서 Native ActiveX로 이벤트 보내기?? [2]파일 다운로드1
943김기룡1/3/201117349닷넷 에러시 조치사항관련... [2]
... 61  62  63  [64]  65  66  67  68  69  70  71  72  73  74  75  ...