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(); }
/// <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)); } }
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; }