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)
1279(non...6/23/201411568(글쓴이의 요청으로 삭제합니다.) [17]
1278이상식6/19/201412786.net DLL 내 자바스크립트를 수정 또는 재정의 할 수 있을까요? [3]
1277김솔지6/18/201410494silverlight에서 datagrid, listbox질문이여 [2]
1276정우석6/16/201410012쿠키 [1]
1274김솔지6/10/201414494배포 페이지 url을 얻고 싶습니다. [8]
1272이훈모6/7/201410054정말 어려운 상황에 직면했습니다. [1]
1270Jong...6/2/201419514C#과 C++을 이용한 Image 처리. [13]
1269김아영5/29/201410227InitializeComponent 함수 호출 지연 현상 [5]
1268솔솔5/27/20149868smart client [1]
1266김솔지5/22/201411949clickonce 수정에 대해 알고싶습니다. [2]
1265이은아5/22/201415555DataGridView 헤더를 두줄이상으로 하고싶습니다. [1]파일 다운로드1
1264김인호5/18/201413157소스코드 및 예제그림 zip 파일 [1]
1263이영종5/15/201411602159페이지 오타인것 같습니다 [5]
1262(non...5/4/201412466(글쓴이의 요청으로 삭제합니다.) [10]
1261이근주5/4/201410772다시 한번 질문드릴께요. [2]
1259이근주5/1/20149819도서 오류인 것 같네요.. [1]
1258최세정4/28/201412231안녕하세요~php module 오류로 고민하다가 여기까지 왔네요..ㅜㅜ [2]
1252popo4/21/201411479바인딩 질문입니다. [2]
1251(non...4/20/201415599(글쓴이의 요청으로 삭제합니다.) [11]
1249홍용규4/17/201415614app.config 파일 관련 질문 있습니다. [2]
1246(non...3/30/201411940(글쓴이의 요청으로 삭제합니다.) [1]
1245POPO3/26/201411347Http 프로토콜 관련 질문 입니다. [1]
1244(non...3/26/201411453(글쓴이의 요청으로 삭제합니다.) [1]
1241(non...3/22/201415229(글쓴이의 요청으로 삭제합니다.) [4]
1240이석주3/21/201418133인터넷 익스플로러가 hang이 걸리는 현상 문의 [1]파일 다운로드1
1238(non...3/13/201411993(글쓴이의 요청으로 삭제합니다.) [2]
... 46  47  48  49  50  51  52  53  54  [55]  56  57  58  59  60  ...