Exemplo n.º 1
0
 public unsafe void Start()
 {
     lock (stateLock)
     {
         if (!Enabled)
         {
             if (IsSupportedNatively(Interval))
             {
                 var caps = new TimeCaps();
                 _ = timeGetDevCaps(ref caps, (uint)sizeof(TimeCaps));
                 var clampedInterval = Math.Clamp((uint)Interval, caps.wPeriodMin, caps.wPeriodMax);
                 _       = timeBeginPeriod(clampedInterval);
                 timerId = timeSetEvent(clampedInterval, 1, callbackDelegate, IntPtr.Zero, EventType.TIME_PERIODIC | EventType.TIME_KILL_SYNCHRONOUS);
                 Enabled = true;
             }
             else
             {
                 Log.Write("Timer", "Unsupported interval detected, will use fallback timer. Expect high CPU usage", LogLevel.Warning);
                 fallbackTimer = new FallbackTimer
                 {
                     Interval = Interval
                 };
                 fallbackTimer.Elapsed += () => Elapsed?.Invoke();
                 fallbackTimer.Start();
                 Enabled = true;
             }
         }
     }
 }
Exemplo n.º 2
0
        private void GetCapabilities(out uint minimum, out uint maximum)
        {
            TimeCaps timeCaps = new TimeCaps(0, 0);
            uint     result   = timeGetDevCaps(out timeCaps, Marshal.SizeOf(timeCaps));

            minimum = timeCaps.minimum;
            maximum = timeCaps.maximum;
        }
Exemplo n.º 3
0
        static Session()
        {
            TimeCaps timeCaps = new TimeCaps();

            NativeMethods.timeGetDevCaps(ref timeCaps, (uint)Marshal.SizeOf(timeCaps));
            periodMax = timeCaps.wPeriodMax;
            periodMin = timeCaps.wPeriodMin;
        }
Exemplo n.º 4
0
        uint GetMinimumTimerResolution()
        {
            var timeCaps = new TimeCaps();
            var devCaps  = InternalNativeMethods.timeGetDevCaps(timeCaps: ref timeCaps, sizeTimeCaps: (uint)Marshal.SizeOf(structure: timeCaps));

            if (devCaps != 0U)
            {
                throw new Exception(message: string.Format(format: "P/Invoke of timeGetDevCaps returned error {0}", arg0: devCaps));
            }
            return(timeCaps.periodMin);
        }
Exemplo n.º 5
0
 public unsafe void Start()
 {
     lock (stateLock)
     {
         var caps = new TimeCaps();
         timeGetDevCaps(ref caps, (uint)sizeof(TimeCaps));
         timeBeginPeriod(Math.Clamp((uint)Interval, caps.wPeriodMin, caps.wPeriodMax));
         Enabled = true;
         timerId = timeSetEvent(1, 1, callbackDelegate, IntPtr.Zero, EventType.TIME_PERIODIC);
     }
 }
Exemplo n.º 6
0
        public static void TimeGetDevCaps(out int minPeriod, out int maxPeriod)
        {
            TimeCaps tc   = new TimeCaps();
            uint     rslt = timeGetDevCaps(ref tc, (uint)TimeCaps.Size);

            if (rslt != 0)
            {
                throw new Win32Exception();
            }
            minPeriod = (int)tc.wPeriodMin;
            maxPeriod = (int)tc.wPeriodMax;
        }
        public MMWaitableTimer(uint period)
        {
            TimeCaps caps = new TimeCaps();
            uint res = timeGetDevCaps(ref caps, (uint)Marshal.SizeOf(typeof(TimeCaps)));

            if (res == 0) {
                timeBeginPeriod(caps.wPeriodMin);
            }
            else {
                Console.WriteLine("could not get timing resolution");
            }

            ev = new AutoResetEvent(false);
            eventID = timeSetEvent(period, 0, ev.SafeWaitHandle.DangerousGetHandle(), IntPtr.Zero, TIME_CALLBACK_EVENT_SET | TIME_PERIODIC);
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        public HighPrecisionEventTimer()
        {
            // Establish the timing intervals capabilities - not needed right now but nice to have.
            TimeCaps timeCaps = new TimeCaps(0, 0);

            // This function queries the timer device to determine its resolution.
            if (timeGetDevCaps(out timeCaps, Marshal.SizeOf(timeCaps)) != 0)
            {
                throw new Exception("HighPrecisionEventTimer failed to aquire parameters from the OS.");
            }

            _minimumValue = timeCaps.Minimum;
            _maximumValue = timeCaps.Maximum;

            _internalAntiGCDelegateInstance = new TimerEventHandler(TickEventHandlerMethod);
        }
        const int TIME_KILL_SYNCHRONOUS = 0x0100; // This flag prevents the event from occurring.

        /// <summary>
        /// Constructor.
        /// </summary>
        public HighPrecisionEventTimer()
        {
            // Establish the timing intervals capabilities - not needed right now but nice to have.
            TimeCaps timeCaps = new TimeCaps(0, 0);

            // This function queries the timer device to determine its resolution.
            if (timeGetDevCaps(out timeCaps, Marshal.SizeOf(timeCaps)) != 0)
            {
                throw new Exception("HighPrecisionEventTimer failed to aquire parameters from the OS.");
            }

            _minimumValue = timeCaps.Minimum;
            _maximumValue = timeCaps.Maximum;

            _internalAntiGCDelegateInstance = new TimerEventHandler(TickEventHandlerMethod);
        }
Exemplo n.º 10
0
		public static void Win32OptimizedStopwatch()
		{
			caps = new TimeCaps();
			if (TimeGetDevCaps(ref caps, (uint)System.Runtime.InteropServices.Marshal.SizeOf(caps)) != 0)
			{
				Console.WriteLine("StopWatch: TimeGetDevCaps failed");
			}
			
			if (TimeBeginPeriod(caps.wPeriodMin) != 0)
			{
				Console.WriteLine("StopWatch: TimeBeginPeriod failed");
			}
		}
 private static extern uint timeGetDevCaps(out TimeCaps timeCaps, int size);
 static extern UInt32 timeGetDevCaps(ref TimeCaps timeCaps,
     UInt32 sizeTimeCaps);
Exemplo n.º 13
0
        private void AdjustMediaTimer(uint ms)
        {
            #region
            //Это нужно только для того, чтоб Thread.Sleep(1) не длилось ~15ms (с дефолтными настройками).
            //Thread.Sleep(1) же нужны для того, чтоб во время "выжидания" временных интервалов не грузился CPU.
            //При DirectShow-превью интервал всегда задаётся в 1ms (при загрузке превью происходит установка, а
            //при выгрузке - сброс, тут же это будет только перед Play и при остановке). На Stopwatch() оно не влияет.
            //DSS2 и bassAudioSource тоже устанавливают интервал в 1ms.
            #endregion

            try
            {
                if (_period == 0 && ms > 0)
                {
                    TimeCaps tcaps = new TimeCaps();
                    if (timeGetDevCaps(ref tcaps, (uint)Marshal.SizeOf(typeof(TimeCaps))) == 0)
                    {
                        uint period = Math.Max(tcaps.wPeriodMin, ms);
                        if (timeBeginPeriod(period) == 0)
                            _period = period;
                    }
                }
                else if (_period > 0 && ms == 0)
                {
                    timeEndPeriod(_period);
                    _period = 0;
                }
            }
            catch (Exception ex)
            {
                SetError(ex);
            }
        }
Exemplo n.º 14
0
 public static extern uint timeGetDevCaps([In, Out] ref TimeCaps timeCaps, [In] uint sizeTimeCaps);
Exemplo n.º 15
0
 internal static extern MMRESULT TimeGetDevCaps(ref TimeCaps timeCaps, uint sizeTimeCaps);
Exemplo n.º 16
0
 public static extern uint TimeGetDevCaps(ref TimeCaps timeCaps, uint sizeTimeCaps);
Exemplo n.º 17
0
        public static void OptimizedMode()
        {
            caps = new TimeCaps();
            if (TimeGetDevCaps(ref caps, (uint)System.Runtime.InteropServices.Marshal.SizeOf(caps)) != 0)
            {
                Debug.ThrowError("StopWatch", "TimeGetDevCaps failed");
            }

            if (TimeBeginPeriod(caps.wPeriodMin) != 0)
            {
                Debug.ThrowError("StopWatch", "TimeBeginPeriod failed");
            }
        }
Exemplo n.º 18
0
 private static extern uint timeGetDevCaps(ref TimeCaps timeCaps, uint sizeTimeCaps);