Click here to Skip to main content
15,903,825 members
Home / Discussions / C#
   

C#

 
GeneralRe: String split Pin
J4amieC23-Oct-09 4:09
J4amieC23-Oct-09 4:09 
QuestionDestructor peculiarity Pin
DaveyM6923-Oct-09 1:25
professionalDaveyM6923-Oct-09 1:25 
AnswerRe: Destructor peculiarity Pin
Pete O'Hanlon23-Oct-09 1:38
mvePete O'Hanlon23-Oct-09 1:38 
GeneralRe: Destructor peculiarity Pin
DaveyM6923-Oct-09 2:20
professionalDaveyM6923-Oct-09 2:20 
GeneralRe: Destructor peculiarity Pin
Pete O'Hanlon23-Oct-09 2:25
mvePete O'Hanlon23-Oct-09 2:25 
GeneralRe: Destructor peculiarity Pin
Gideon Engelberth23-Oct-09 7:47
Gideon Engelberth23-Oct-09 7:47 
GeneralRe: Destructor peculiarity Pin
DaveyM6923-Oct-09 9:44
professionalDaveyM6923-Oct-09 9:44 
GeneralRe: Destructor peculiarity Pin
DaveyM6923-Oct-09 9:52
professionalDaveyM6923-Oct-09 9:52 
GeneralRe: Destructor peculiarity Pin
cmk23-Oct-09 5:21
cmk23-Oct-09 5:21 
GeneralRe: Destructor peculiarity Pin
DaveyM6923-Oct-09 8:10
professionalDaveyM6923-Oct-09 8:10 
GeneralRe: Destructor peculiarity Pin
DaveyM6923-Oct-09 9:48
professionalDaveyM6923-Oct-09 9:48 
AnswerRe: Destructor peculiarity Pin
Gideon Engelberth23-Oct-09 7:55
Gideon Engelberth23-Oct-09 7:55 
GeneralRe: Destructor peculiarity Pin
DaveyM6923-Oct-09 9:40
professionalDaveyM6923-Oct-09 9:40 
AnswerRe: Destructor peculiarity Pin
Luc Pattyn27-Oct-09 3:36
sitebuilderLuc Pattyn27-Oct-09 3:36 
GeneralRe: Destructor peculiarity Pin
DaveyM6927-Oct-09 7:04
professionalDaveyM6927-Oct-09 7:04 
GeneralRe: Destructor peculiarity Pin
Luc Pattyn27-Oct-09 7:09
sitebuilderLuc Pattyn27-Oct-09 7:09 
GeneralRe: Destructor peculiarity Pin
DaveyM6927-Oct-09 10:45
professionalDaveyM6927-Oct-09 10:45 
GeneralRe: Destructor peculiarity Pin
Luc Pattyn27-Oct-09 11:17
sitebuilderLuc Pattyn27-Oct-09 11:17 
GeneralRe: Destructor progress report 1 Pin
Luc Pattyn27-Oct-09 17:20
sitebuilderLuc Pattyn27-Oct-09 17:20 
GeneralRe: Destructor progress report 1 Pin
DaveyM6928-Oct-09 8:58
professionalDaveyM6928-Oct-09 8:58 
GeneralRe: Destructor progress report 1 Pin
Luc Pattyn28-Oct-09 9:54
sitebuilderLuc Pattyn28-Oct-09 9:54 
GeneralRe: Destructor progress report 1 Pin
DaveyM6928-Oct-09 10:52
professionalDaveyM6928-Oct-09 10:52 
GeneralRe: Destructor progress report 1 Pin
DaveyM6928-Oct-09 22:19
professionalDaveyM6928-Oct-09 22:19 
After sleeping on it, I'm pretty sure that the callback is the source of the issues that prevent me from using the finalizer (either with or without Dispose) to close the port. It's clearer to see in code! Is there any reliable way to prolong the life of a delegate instance?
C#
using System;
using System.Runtime.InteropServices;

public class Output
{
    private const int CALLBACK_FUNCTION = 0x00030000;
    private const int MOM_CLOSE = 0x3C8;
    private const int MOM_OPEN = 0x3C7;

    private delegate void MidiProc(
        IntPtr hMidi,
        int wMsg,
        int dwInstance,
        int dwParam1,
        int dwParam2);

    [DllImport("winmm.dll")]
    private static extern int midiOutClose(
        IntPtr hmo
        );

    [DllImport("winmm.dll")]
    private static extern int midiOutOpen(
        out IntPtr lphmo,
        int uDeviceID,
        MidiProc dwCallback,
        int dwCallbackInstance,
        int dwFlags
        );

    private MidiProc callback;
    private IntPtr handle;
    private int id;

    public Output(int id)
    {
        callback = OnCallback;
        handle = IntPtr.Zero;
        this.id = id;
    }
    ~Output() // Unreliable as no way to determine when this will be called
    {
        Close();
    } /* MIDI sub sytem will now use callback to send MIM_CLOSE if
       * Output was open but callback may have been collected */

    public void Close()
    {
        if (handle != IntPtr.Zero)
        {
            midiOutClose(handle);
            handle = IntPtr.Zero;
        }
    }
    private void OnCallback(
        IntPtr hMidi, int wMsg, int dwInstance, int dwParam1, int dwParam2)
    {
        // ...
    }
    public void Open()
    {
        if (handle == IntPtr.Zero)
            midiOutOpen(
                out handle, id, callback, id, CALLBACK_FUNCTION);
    }
}


Dave

"My code works, but I don't understand why!" - DaveyM69 (Me)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)

GeneralRe: Destructor progress report 1 Pin
DaveyM6928-Oct-09 9:35
professionalDaveyM6928-Oct-09 9:35 
QuestionTheading problem Pin
Rick van Woudenberg23-Oct-09 0:52
Rick van Woudenberg23-Oct-09 0:52 

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.