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]

... 46  47  48  [49]  50  51  52  53  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
3649kokon11/17/201521097예제 파일 실행이 안 되네요 [5]
3647Sang...11/15/201518875Part 3 목차? [5]
3646힘찬도약11/13/201524831c# mscorlib System.IO IOException [8]파일 다운로드2
3644힘찬도약11/11/201523424c# user.config파일 [2]
3645spow...11/13/201519837    답변글 [답변]: c# user.config파일 - Json.NET을 이용한 설정파일 처리 [1]파일 다운로드1
3643힘찬도약11/11/201521286C# 함수의 processing time과 재호출 [14]
3642.net11/10/201520776c# 으로 작성된 com+ 에 대한 문제입니다. [2]
3641힘참도약11/9/201520415c# log file 관련해서 질문드립니다. [5]
3638윤창선11/4/201523227사설IP가 부여된 무선라우터간 영상전송 관련 문의 [8]
3634Hyun...11/2/201519795c# 에서 webkit browser에서 webgl을 이용하는 사이트에 접속이 안됩니다. [1]
3633힘찬도약10/31/201519961mysql insert where not exists [6]
3632힘찬도약10/27/201519626C# Lock 관련해서 질문드립니다. [6]
3655iwc11/30/201517831    답변글 [답변]: C# Lock 관련해서 질문드립니다.
3631강준10/26/201521886iis 8.5 preload 기능에 대해 질문이 있습니다. [9]
3630김정훈10/25/201520431몬티홀 게임 관련 질문 [1]
3629pooq10/23/201520633리플렉션 관련해서 질문 입니다. [3]
3628최영민10/22/201518720스마트 클라이언트 로딩속도 문의입니다. [3]
3627양주호10/22/201518593C#으로 컨버팅 하려고 하는데요... [1]
3626조성진10/21/201519456책보고 첫번째 예제부터 문제가 생기네요 ^^; [4]파일 다운로드1
3623Bere...10/19/201520412질문이라기 보단... [2]
3625Bere...10/20/201518800    답변글 [답변]: 질문이라기 보단... [2]파일 다운로드1
3621힘찬도약10/18/201519115[C# 6.0]multi threading과 ui control [9]
3624힘찬도약10/19/201519352    답변글 [답변]: [C# 6.0]multi threading과 ui control [6]파일 다운로드1
3620popo10/13/201517706WPF의 datagrid, listview 컨트롤 관련 질문 입니다. [1]
3619링크의 ...10/12/201522511OCX 로드 관련 질문입니다. [5]파일 다운로드1
3616수요일밥...10/7/201523823몇 가지 오류 (2) [6]
... 46  47  48  [49]  50  51  52  53  54  55  56  57  58  59  60  ...