المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Cleaning up members of a static array property on app exit [modified]



C# Programming
05-29-2009, 01:14 AM
I'm working on a managed MIDI wrapper. Because the only valid devies are those that ae installed, the wrapper exposes a static public property that is an array of devices that are instances of the Input or Output classes (it is not possible for them to create their own instances of these classes).

It's extremely important that any devices used are stopped and closed etc correctly before the program closes.

To do this, I've made the Devices[] property getter return a singleton's static property that is the array, and put the necessary logic in the singleton's destructor.

Is this the best approach?

public class Output
{
private class Singleton
{
static Singleton()
{
Output[] result = new Output[GetNumberOfDevices()];
if (result.Length > 0)
{
for (UInt32 u = 0; u < result.Length; u++)
{
result[u] = new Output(u);
result[u].SetDeviceCapabilities();
}
}
Devices = result;
}

internal static Output[] Devices { get; private set; }

~Singleton()
{
Output.CloseAll();
}
}

internal Output(UInt32 id)
{
// ...
}
public static Output[] Devices
{
get { return Singleton.Devices; }
}
public new static void CloseAll()
{
foreach (Output device in Devices)
device.Close();
}
public override void Close()
{
// Logic here
}
internal static UInt32 GetNumberOfDevices()
{
return InteropFunctions.midiOutGetNumDevs();
}
internal void SetDeviceCapabilities()
{
// ...
}
// ...
}


Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn (http://www.codeproject.com/Members/Luc-Pattyn))
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia (http://uncyclopedia.org/wiki/.NET))
Why are you using VB6? Do you hate yourself? (Christian Graus (http://www.codeproject.com/script/Membership/View.aspx?mid=6556))

modified on Thursday, May 28, 2009 4:54 PM