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)
1288박주만7/8/201430875C# 서비스 기반 데이터베이스(mdf) & InstallShield Limited Edition 설치 및 배포 [2]파일 다운로드1
1287김용환7/8/201428753오라클 db 사용관련 문의입니다. [4]파일 다운로드1
1286C#조으다7/8/201418700WebBrowser 공유기 관리 웹 페이지 인증 [3]
1285C#조으다7/5/201418988IE DocumentComplete 이벤트가 발생되지 않습니다. [2]
1284(non...7/4/201419336(글쓴이의 요청으로 삭제합니다.) [3]
1283김영대7/3/201422199안녕하십니까 정성태님 죄송하지만 SmartClient 에 관한 질문이 있습니다. [9]
1282(non...7/2/201419159(글쓴이의 요청으로 삭제합니다.) [2]
1281(non...7/1/201420338(글쓴이의 요청으로 삭제합니다.) [4]
1280동동이6/25/201419988안녕하세요. ocx의 비동기 또는 쓰레드에서 호출 [1]
1279(non...6/23/201420338(글쓴이의 요청으로 삭제합니다.) [17]
1278이상식6/19/201421690.net DLL 내 자바스크립트를 수정 또는 재정의 할 수 있을까요? [3]
1277김솔지6/18/201418014silverlight에서 datagrid, listbox질문이여 [2]
1276정우석6/16/201417720쿠키 [1]
1274김솔지6/10/201422171배포 페이지 url을 얻고 싶습니다. [8]
1272이훈모6/7/201416749정말 어려운 상황에 직면했습니다. [1]
1270Jong...6/2/201427319C#과 C++을 이용한 Image 처리. [13]
1269김아영5/29/201417802InitializeComponent 함수 호출 지연 현상 [5]
1268솔솔5/27/201417239smart client [1]
1266김솔지5/22/201419656clickonce 수정에 대해 알고싶습니다. [2]
1265이은아5/22/201424147DataGridView 헤더를 두줄이상으로 하고싶습니다. [1]파일 다운로드1
1264김인호5/18/201422978소스코드 및 예제그림 zip 파일 [1]
1263이영종5/15/201419677159페이지 오타인것 같습니다 [5]
1262(non...5/4/201420440(글쓴이의 요청으로 삭제합니다.) [10]
1261이근주5/4/201417967다시 한번 질문드릴께요. [2]
1259이근주5/1/201417891도서 오류인 것 같네요.. [1]
1258최세정4/28/201420512안녕하세요~php module 오류로 고민하다가 여기까지 왔네요..ㅜㅜ [2]
... 46  47  48  49  50  51  52  53  54  55  [56]  57  58  59  60  ...