Can not remove event handler of MultiSourceFrameArrived RRS feed

  • Question

  • Hi everyone.

    I'm developing KINECT application with C#.

    I want to fire an event when MultiSourceFrame is arrived just first time. I wrote code like followings...

    private EventHandler<MultiSourceFrameArrivedEventArgs> handler = null;
    public void Ready()
        // setup multisoruceframe
        kinectReader = KinectSensor.OpenMultiSourceFrameReader(frameTypes);
        // setup event halder
        handler = (sender, e) =>
            // do something
            // and remove handler
            kinectReader.MultiSourceFrameArrived -= handler;
        // add handler
        kinectReader.MultiSourceFrameArrived += handler;

    but handler is not removed.

    Is MultiSourceFrameArrived's remove accessor working ?


    • Edited by f.ito Wednesday, December 24, 2014 1:57 AM
    Wednesday, December 24, 2014 1:56 AM

All replies

  • You code looks somewhat ok. I'll take a guess here if it's not working, I think you're running into a TLS (Thread local storage) issue. The KinectReader object that runs inside your handler delegate is a copied object running on another thread and may not necessarily point to the original instance of KinectReader which actually contains the MultiSourceFrameArrived event handler mapping.

    Try this and see if this helps.

    Instead of using the copied object KinectReader inside your delegate, convert sender to the KinectReader object instead and remove the multisource frame arrived event handler item from the list.

     // setup event handler
        handler = (sender, e) =>
            var kinectReader = (MultiSourceFrameReader)sender;
            // do something
            // and remove handler
            kinectReader.MultiSourceFrameArrived -= this.handler;

    Sr. Enterprise Architect | Trainer | Consultant | MCT | MCSD | MCPD | SharePoint TS | MS Virtual TS |Windows 8 App Store Developer | Linux Gentoo Geek | Raspberry Pi Owner | Micro .Net Developer | Kinect For Windows Device Developer |blog:

    Thursday, December 25, 2014 1:45 AM