Ejemplo n.º 1
0
        public void Start()
        {
            if (IsRunning)
            {
                return;
            }

            if (_timerId == NoTimerId)
            {
                var timeCaps = default(MidiTimerWinApi.TIMECAPS);
                ProcessMmResult(MidiTimerWinApi.timeGetDevCaps(ref timeCaps, (uint)Marshal.SizeOf(timeCaps)));

                _resolution   = Math.Min(Math.Max(timeCaps.wPeriodMin, _interval), timeCaps.wPeriodMax);
                _tickCallback = OnTick;

                ProcessMmResult(MidiTimerWinApi.timeBeginPeriod(_resolution));
                _timerId = MidiTimerWinApi.timeSetEvent(_interval, _resolution, _tickCallback, IntPtr.Zero, MidiTimerWinApi.TIME_PERIODIC);
                if (_timerId == 0)
                {
                    var errorCode = Marshal.GetLastWin32Error();
                    throw new MidiDeviceException("Unable to initialize MIDI clock.", new Win32Exception(errorCode));
                }
            }

            _stopwatch.Start();
        }
Ejemplo n.º 2
0
        public void Stop()
        {
            StopInternalTimer();

            MidiTimerWinApi.timeEndPeriod(_resolution);
            MidiTimerWinApi.timeKillEvent(_timerId);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Starts a tick generator.
        /// </summary>
        /// <param name="interval">Interval between ticks.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="interval"/> is out of
        /// [<see cref="MinInterval"/>; <see cref="MaxInterval"/>] range.</exception>
        protected override void Start(TimeSpan interval)
        {
            ThrowIfArgument.IsOutOfRange(nameof(interval),
                                         interval,
                                         MinInterval,
                                         MaxInterval,
                                         $"Interval is out of [{MinInterval}, {MaxInterval}] range.");

            var intervalInMilliseconds = (uint)interval.TotalMilliseconds;

            var timeCaps = default(MidiTimerWinApi.TIMECAPS);

            ProcessMmResult(MidiTimerWinApi.timeGetDevCaps(ref timeCaps, (uint)Marshal.SizeOf(timeCaps)));

            _resolution   = Math.Min(Math.Max(timeCaps.wPeriodMin, intervalInMilliseconds), timeCaps.wPeriodMax);
            _tickCallback = OnTick;

            ProcessMmResult(MidiTimerWinApi.timeBeginPeriod(_resolution));
            _timerId = MidiTimerWinApi.timeSetEvent(intervalInMilliseconds, _resolution, _tickCallback, IntPtr.Zero, MidiTimerWinApi.TIME_PERIODIC);
            if (_timerId == NoTimerId)
            {
                var errorCode = Marshal.GetLastWin32Error();
                throw new MidiDeviceException("Unable to start tick generator.", new Win32Exception(errorCode));
            }
        }
Ejemplo n.º 4
0
        public void Stop()
        {
            IsRunning = false;

            // TODO: process errors

            _stopwatch.Stop();
            MidiTimerWinApi.timeEndPeriod(_resolution);
            MidiTimerWinApi.timeKillEvent(_timerId);
        }
Ejemplo n.º 5
0
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
            }

            if (_timerId != NoTimerId)
            {
                MidiTimerWinApi.timeEndPeriod(_resolution);
                MidiTimerWinApi.timeKillEvent(_timerId);
            }

            _disposed = true;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Releases all resources used by the current tick generator.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
            }

            if (IsRunning)
            {
                MidiTimerWinApi.timeEndPeriod(_resolution);
                MidiTimerWinApi.timeKillEvent(_timerId);
            }

            _disposed = true;
        }
Ejemplo n.º 7
0
        public void Start()
        {
            if (IsRunning)
            {
                return;
            }

            // TODO: process errors

            var timeCaps = default(MidiTimerWinApi.TIMECAPS);

            MidiTimerWinApi.timeGetDevCaps(ref timeCaps, (uint)Marshal.SizeOf(timeCaps));

            _resolution   = Math.Min(Math.Max(timeCaps.wPeriodMin, _interval), timeCaps.wPeriodMax);
            _tickCallback = OnTick;

            _stopwatch.Start();

            MidiTimerWinApi.timeBeginPeriod(_resolution);
            _timerId = MidiTimerWinApi.timeSetEvent(_interval, _resolution, _tickCallback, IntPtr.Zero, MidiTimerWinApi.TIME_PERIODIC);

            IsRunning = true;
        }
 /// <summary>
 /// Stops a tick generator.
 /// </summary>
 protected override void Stop()
 {
     MidiTimerWinApi.timeEndPeriod(_resolution);
     MidiTimerWinApi.timeKillEvent(_timerId);
 }