Click here to Skip to main content
15,887,214 members
Home / Discussions / C#
   

C#

 
AnswerRe: Do something every t seconds Pin
Christian Graus28-May-09 19:27
protectorChristian Graus28-May-09 19:27 
AnswerRe: Do something every t seconds Pin
Mirko198029-May-09 4:19
Mirko198029-May-09 4:19 
Questionreally wierd think Pin
funic28-May-09 14:36
funic28-May-09 14:36 
Questioncreate setup package Pin
sakis2428-May-09 10:50
sakis2428-May-09 10:50 
AnswerRe: create setup package Pin
EliottA28-May-09 11:45
EliottA28-May-09 11:45 
QuestionCleaning up members of a static array property on app exit [modified] Pin
DaveyM6928-May-09 10:42
professionalDaveyM6928-May-09 10:42 
AnswerRe: Cleaning up members of a static array property on app exit Pin
Kythen28-May-09 13:36
Kythen28-May-09 13:36 
GeneralRe: Cleaning up members of a static array property on app exit [modified] Pin
DaveyM6929-May-09 10:36
professionalDaveyM6929-May-09 10:36 
I was avoiding Dispose as calling my Close automatically frees unmanaged resources - but on reflection, it makes sense.

By the way, the 'real' classes derive from an abstract base class, so I've implemented the Dispose in there.

I've experimented with a 'manager' class, and this is what I have as a basic outline. Opinions welcome!
public static class MIDIDeviceManager
{
    private static List<MIDIOutput> activeOutputs = new List<MIDIOutput>();
    private static List<MIDIInput> activeInputs = new List<MIDIInput>();

    private static UInt32 availableMidiOutputCount;
    private static UInt32 availableMidiInputCount;

    public static UInt32 AvailableMIDIOutputCount
    {
        get
        {
            if (availableMidiOutputCount == 0)
                availableMidiOutputCount = InteropFunctions.midiOutGetNumDevs();
            return availableMidiOutputCount;
        }
    }
    public static UInt32 AvailableMIDIInputCount
    {
        get
        {
            if (availableMidiInputCount == 0)
                availableMidiInputCount = InteropFunctions.midiInGetNumDevs();
            return availableMidiInputCount;
        }
    }

    public static IList<MIDIOutput> ActiveOutputs
    {
        get { return activeOutputs.AsReadOnly(); }
    }
    public static IList<MIDIInput> ActiveInputs
    {
        get { return activeInputs.AsReadOnly(); }
    }

    public static MIDIOutput GetMIDIOutput(UInt32 id)
    {
        if (id < availableMidiOutputCount)
        {
            MIDIOutput result = null;
            foreach (MIDIOutput midiOutput in activeOutputs)
            {
                if (midiOutput.ID == id)
                {
                    result = midiOutput;
                    break;
                }
            }
            if (result == null)
            {
                result = new MIDIOutput(id);
                activeOutputs.Add(result);
            }
            return result;
        }
        throw new ArgumentOutOfRangeException(
            "id", "ID out of range. Check MIDIOutputCount for available range.");
    }
    public static MIDIInput GetMIDIInput(UInt32 id)
    {
        if (id < availableMidiInputCount)
        {
            MIDIInput result = null;
            foreach (MIDIInput midiInput in activeInputs)
            {
                if (midiInput.ID == id)
                {
                    result = midiInput;
                    break;
                }
            }
            if (result == null)
            {
                result = new MIDIInput(id);
                activeInputs.Add(result);
            }
            return result;
        }
        throw new ArgumentOutOfRangeException(
            "id", "ID out of range. Check MIDIInputCount for available range.");
    }

    internal static void RemoveDevice(MIDIDevice device)
    {
        switch (device.DeviceType)
        {
            case DeviceTypes.Input:
                activeInputs.Remove((MIDIInput)device);
                break;
            default: // Output
                activeOutputs.Remove((MIDIOutput)device);
                break;
        }
    }
}
public abstract class MIDIDevice : IDisposable
{
    private object syncRoot = new object();
    private bool isDisposed = false;

    public MIDIDevice(UInt32 id)
    {
        ID = id;
    }

    ~MIDIDevice()
    {
        Dispose(false);
    }

    public UInt32 ID { get; private set; }
    public string Name { get; internal set; }

    public abstract DeviceTypes DeviceType { get; }

    public void Close()
    {
        // ...
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        lock (syncRoot)
        {
            if (!isDisposed)
            {
                if (disposing)
                {
                    Close();
                    MIDIDeviceManager.RemoveDevice(this);
                }
            }
            isDisposed = true;
        }
    }
}
public class MIDIOutput : MIDIDevice
{
    internal MIDIOutput(UInt32 id) : base(id) { }

    public override DeviceTypes DeviceType
    {
        get { return DeviceTypes.Output; }
    }
}
public class MIDIInput : MIDIDevice
{
    internal MIDIInput(UInt32 id) : base(id) { }

    public override DeviceTypes DeviceType
    {
        get { return DeviceTypes.Input; }
    }
}
public enum DeviceTypes
{
    Output = 0,
    Input = 1,
}


Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)

QuestionExecuting Application After Installation (Using Windows Installer) Pin
BlitzPackage28-May-09 9:29
BlitzPackage28-May-09 9:29 
Generalhelp for run length encoding Pin
ali rezaiy28-May-09 8:56
ali rezaiy28-May-09 8:56 
GeneralRe: help for run length encoding Pin
harold aptroot28-May-09 10:59
harold aptroot28-May-09 10:59 
GeneralRe: help for run length encoding Pin
Alan Balkany28-May-09 11:06
Alan Balkany28-May-09 11:06 
QuestionWhat are your preferences for work with databases ADO.net 2.0 LINQ TO SQL or Entity Framework Pin
ToddHileHoffer28-May-09 7:45
ToddHileHoffer28-May-09 7:45 
AnswerRe: What are your preferences for work with databases ADO.net 2.0 LINQ TO SQL or Entity Framework Pin
PIEBALDconsult28-May-09 8:22
mvePIEBALDconsult28-May-09 8:22 
AnswerRe: What are your preferences for work with databases ADO.net 2.0 LINQ TO SQL or Entity Framework Pin
Henry Minute28-May-09 8:43
Henry Minute28-May-09 8:43 
GeneralRe: What are your preferences for work with databases ADO.net 2.0 LINQ TO SQL or Entity Framework Pin
ScottM128-May-09 20:21
ScottM128-May-09 20:21 
QuestionDraggable WinForm w/ Docked Control Problem Pin
baehoo28-May-09 7:42
baehoo28-May-09 7:42 
QuestionUsing sortedlist or sorteddictionary Pin
michaelgr128-May-09 7:10
michaelgr128-May-09 7:10 
AnswerRe: Using sortedlist or sorteddictionary Pin
Ennis Ray Lynch, Jr.28-May-09 7:23
Ennis Ray Lynch, Jr.28-May-09 7:23 
GeneralRe: Using sortedlist or sorteddictionary Pin
michaelgr128-May-09 7:24
michaelgr128-May-09 7:24 
GeneralRe: Using sortedlist or sorteddictionary Pin
Ennis Ray Lynch, Jr.28-May-09 7:29
Ennis Ray Lynch, Jr.28-May-09 7:29 
GeneralRe: Using sortedlist or sorteddictionary Pin
DaveyM6928-May-09 7:31
professionalDaveyM6928-May-09 7:31 
AnswerRe: Using sortedlist or sorteddictionary Pin
Luc Pattyn28-May-09 7:29
sitebuilderLuc Pattyn28-May-09 7:29 
GeneralRe: Using sortedlist or sorteddictionary Pin
michaelgr128-May-09 7:35
michaelgr128-May-09 7:35 
GeneralRe: Using sortedlist or sorteddictionary [modified] Pin
Luc Pattyn28-May-09 7:39
sitebuilderLuc Pattyn28-May-09 7:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.