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]

... [16]  17  18  19  20  21  22  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
5537민성8/6/20215305안녕하세요 WPF에서 이미 있는창이 있다면 안띠우게 하는 방법 [1]
55358/5/20216833안녕하세요. 초보 웹 개발자입니다. [10]파일 다운로드2
5534하영7/28/20215355clrprofiler 를 사용하여 세션정보 접근 [6]파일 다운로드1
5533함준혁7/20/20214850.net fpspread 관련 질문입니다.. [1]
5532조윤상7/15/20216209바인딩은 성공 했습니다. 그런데 브라우저에서 인증서가 없다고 나옵니다. [2]
5530ocm7/14/20215822pthread_create [7]파일 다운로드1
5529ksc7/13/20214988Source Generator 관련 질문이 있습니다. [1]
5528초심으로7/9/20216160MDI 에서 USB 연결해제 알림이 안되는 문제 질문 드려봅니다. [7]파일 다운로드1
5527wuny7/7/20215064제어관련 고민을하다가 소캣방식 선택 [2]
5526이성열 donator7/7/20216647wpf x64로 만든 메인 프로그램에서 dll로 된 UserControl 속성이 디자인타임에 잘 안보이는 문제 [10]파일 다운로드2
5525Wuny7/7/20217206제가 만든 배포파일은 window에서 막는걸까요? [2]파일 다운로드1
5524하이스컬7/2/20216503특정 이벤트에서 다른 이벤트 호출 관련 문의 [3]
5523민우7/1/202112240도커 사용시 윈도우 이미지 생성도 가능한가요? [2]
5522질문6/28/20216926WPF에서 splash screen이 나타나는 위치를 변경할 수 있나요? [3]
5521김민혁6/24/20217029.exe 파일 에러에 관한 질문 입니다. [3]
5520한예지 donator6/21/20216796랜덤함수 질문있습니다!! [2]
5519리얼킴6/19/20215750.net framwork 4.0 에서 4.8로 꼭 가야할까요?? [1]
5518한예지 donator6/18/20216461ArrayList, IList에 대해 질문 있습니다. [3]
5517wunsy6/18/20216895winform에서 Button 활성화, 비활성화 [4]
5516ocm6/15/20215893mips 어셈블리 연산 다시 질문드려요 (첨부파일 갱신됨) [2]파일 다운로드1
5515ocm6/14/20216779mips 어셈블리 연산 [6]파일 다운로드1
5514jongs6/11/20217496GethashCode와 String대한 질문 [2]
5513labe...6/11/20217222C# Winform 에서 Label에 동일한 Color를 넣었을 때 처리방법이 궁금합니다. [2]
55126/11/20218003xlwings 가 실행조차 되지 않습니다. ㅠㅠ [7]
5511Syong6/9/20216812User Control에 string array 속성 추가하는 방법 [4]
5510jay6/8/20215834string 문자열에 쌍따옴표(")를 넣고 싶습니다. [1]파일 다운로드1
... [16]  17  18  19  20  21  22  23  24  25  26  27  28  29  30  ...