/// <summary> /// Modifies the <see cref="P:Status"/> value. /// </summary> /// <param name="addStatus">One or more status flags to add.</param> /// <param name="removeStatus">One or more status flags to remove.</param> /// <exception cref="MidiPortException">Thrown when the resulting status would be invalid.</exception> /// <remarks> /// The following status flag combinations are invalid: /// <see cref="P:Status"/> cannot equal <see cref="MidiPortStatus.None"/>. /// <see cref="P:Status"/> cannot contain any other flags when set to <see cref="MidiPortStatus.Closed"/>. /// <see cref="P:Status"/> cannot contain both <see cref="MidiPortStatus.Started"/> and /// <see cref="MidiPortStatus.Stopped"/> or <see cref="MidiPortStatus.Paused"/>. /// </remarks> protected void ModifyStatus(MidiPortStatus addStatus, MidiPortStatus removeStatus) { ThrowIfDisposed(); MidiPortStatus portStatus = Status; // automatically clear the Reset status. portStatus &= ~(removeStatus | MidiPortStatus.Reset); portStatus |= addStatus; // validate modifications if (portStatus == MidiPortStatus.None) { throw new MidiPortException( String.Format(CultureInfo.InvariantCulture, Properties.Resources.MidiPort_InvalidStatus, portStatus)); } // closed cannot be combined with another status if (((portStatus & MidiPortStatus.Closed) > 0) && (portStatus != MidiPortStatus.Closed)) { throw new MidiPortException( String.Format(CultureInfo.InvariantCulture, Properties.Resources.MidiPort_InvalidStatus, portStatus)); } // cannot be started and stopped or paused at the same time. if (((portStatus & MidiPortStatus.Started) > 0) && (((portStatus & MidiPortStatus.Stopped) > 0) || ((portStatus & MidiPortStatus.Paused) > 0))) { throw new MidiPortException( String.Format(CultureInfo.InvariantCulture, Properties.Resources.MidiPort_InvalidStatus, portStatus)); } Status = portStatus; }
/// <summary> /// Removes any references the receiver component has to/from the Midi Port. /// </summary> /// <param name="port">A Midi In Port. Must not be null.</param> public void Uninitialize(IMidiPort port) { Check.IfArgumentNull(port, "port"); Check.IfArgumentNotOfType <MidiInPort>(port, "port"); port.StatusChanged -= new EventHandler(MidiPort_StatusChanged); PortStatus = MidiPortStatus.None; }
/// <summary> /// Manages turning the timer on and off based on the port status. /// </summary> /// <param name="newStatus">The new port status to be set.</param> protected override void OnNewPortStatus(MidiPortStatus newStatus) { var oldStatus = PortStatus; base.OnNewPortStatus(newStatus); if (oldStatus != newStatus) { switch (newStatus) { case MidiPortStatus.Open: _timer.StartTimer(); break; case MidiPortStatus.Closed: _timer.StopTimer(); break; } } }
/// <summary> /// Manages starting and stopping the extra (thread-pool) thread used to read the queue. /// </summary> /// <param name="newStatus">The new port status to be set.</param> protected override void OnNewPortStatus(MidiPortStatus newStatus) { var oldStatus = PortStatus; base.OnNewPortStatus(newStatus); if (oldStatus != newStatus) { switch (newStatus) { case MidiPortStatus.Open: ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncReadLoop)); break; case MidiPortStatus.Closed: // signal to exit worker thread _queue.SignalWait(); break; } } }
/// <summary> /// Queries the port <see cref="P:Status"/> if one or more of the /// specified <see cref="MidiPortStatus"/> flags are present. /// </summary> /// <param name="portStatus">One or more status flags to query.</param> /// <returns>Returns true if one or more of the <see cref="MidiPortStatus"/> /// flags is set in the <see cref="P:Status"/> property.</returns> public bool HasStatus(MidiPortStatus portStatus) { return((Status & portStatus) > 0); }
/// <summary> /// Handles the new port status. /// </summary> /// <param name="newStatus">The new port status value.</param> protected virtual void OnNewPortStatus(MidiPortStatus newStatus) { PortStatus = newStatus; }