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]

1  2  [3]  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
5920한예지 donator10/3/202313706C#과 WIN32 API 관계 질문드립니다. [4]
5919이건우9/27/202312380WinForm의 로딩속도 관련 질문입니다 [2]
5917한예지 donator9/14/202313096동기화 도구 질문 있습니다. [4]
5916한예지 donator9/3/202313491Thread.Sleep(500), await Task.Delay(500), Task.Delay(500) 차이점이 궁금합니다. [2]
5915한예지 donator8/30/202314911비동기 코드를 for 문 안에 작성한 경우 제어 변수가 올바르게 동작하지 않는 이유가 궁금합니다. [3]
5914한상욱8/11/202314448.net wpf에서 skiasharp 의 skelement 를 canvas로 사용 하고 있습니다. [1]
5913김태우8/10/202314022지역변수로 이해하는 메서드매개변수 게시글 댓글 [3]
5912guest4/25/202319487[참고 - 초보용] Sqlite 디비는 double이 없고 Real이 대신합니다 [3]
5911guest4/24/202313212Form1.cs와 외부 class.cs와 통신 (static async method포함) [4]파일 다운로드1
5910guest4/24/202312557Async 메서드와 try~catch [1]
5909guest4/22/202314174Visual Studio 구매 시(1인 개발자) [4]
5908guest4/22/202313399텅빈 원그리기 [5]
5907민성4/21/202313398안녕하세요 서버 백업 문제에 대해서 [2]
5906guest4/21/202313663Dispatcher 서비스 구현 질문 [1]
5905guest4/20/202314735tabControl의 tabPage가 여러 개일 때 순서를 바꾸기가 까다롭네요 [5]
5904guest4/18/202315047[신규자료첨부] DLL 'SQLite.Interop.dll'을 찾을 수 없습니다 [4]파일 다운로드1
5903guest4/18/202313669fileSystemWatcher 이벤트 관련 질문입니다 [2]
5902guest4/17/202314817c#으로 USB 관련 질문 [2]
5901guest4/17/202312081내솔루션 판매 시 1.0.0.0 폴더와 Sqlite 배포 [5]
5900guest4/17/202315223DLL 'SQLite.Interop.dll'을 찾을 수 없습니다 [2]파일 다운로드1
5899guest4/17/202313203Dictionary와 Linq [4]
5898차가워4/17/202312792CNTK 교육 문의 [1]
5897guest4/17/202312391Socket스레드와 UI thread [4]
5896HAN4/16/202312460c언어 return 에 대해 궁금한게 있어요. [1]파일 다운로드1
5895guest4/15/202312703Drag and Drop - 모든 컨트롤 [2]
5894송부장4/14/202314659[질문] Visual Studio 2022에서 '도구 상자 항목 선택'의 'COM 구성 요소' 탭에서 ActiveX 목록이 보이지 않습니다. [3]파일 다운로드2
1  2  [3]  4  5  6  7  8  9  10  11  12  13  14  15  ...