9

I'm in the process of converting my Microsoft SDK Beta code to the Microsoft SDK Official Release that was released February 2012.

I added a generic PauseKinect() to pause the Kinect. My pause will really only remove the event handler that updated the image

Pros:

  • No Reinitialization (30+ second wait time)

Cons:

  • Kinect still processing images

Pause Method (Color Only)

internal void PauseColorImage(bool isPaused)
{
    if (isPaused)
    {
        _Kinect.ColorFrameReady -= ColorFrameReadyEventHandler;
        //_Kinect.ColorStream.Disable();
    }

    else
    {
        _Kinect.ColorFrameReady += ColorFrameReadyEventHandler;
        //_Kinect.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
    }
}

PROBLEM:

Even though I'm removing the event why is it still getting triggered?

NOTE:

Also when I pause the color image I'm also pausing the depth and skeleton in their object.

SIDE NOTE:

If I uncomment my code it works fine, but then it'll take forever to reinitialize which is not what I want to do.

MS in Reflector

public void AddHandler(EventHandler<T> originalHandler)
{
    if (originalHandler != null)
    {
        this._actualHandlers.Add(new ContextHandlerPair<T, T>(originalHandler, SynchronizationContext.Current));
    }
}

public void RemoveHandler(EventHandler<T> originalHandler)
{
    SynchronizationContext current = SynchronizationContext.Current;
    ContextHandlerPair<T, T> item = null;
    foreach (ContextHandlerPair<T, T> pair2 in this._actualHandlers)
    {
        EventHandler<T> handler = pair2.Handler;
        SynchronizationContext context = pair2.Context;
        if ((current == context) && (handler == originalHandler))
        {
            item = pair2;
            break;
        }
    }
    if (item != null)
    {
        this._actualHandlers.Remove(item);
    }
}


public void Invoke(object sender, T e)
{
    if (this.HasHandlers)
    {
        ContextHandlerPair<T, T>[] array = new ContextHandlerPair<T, T>[this._actualHandlers.Count];
        this._actualHandlers.CopyTo(array);
        foreach (ContextHandlerPair<T, T> pair in array)
        {
            EventHandler<T> handler = pair.Handler;
            SynchronizationContext context = pair.Context;
            if (context == null)
            {
                handler(sender, e);
            }
            else if (this._method == ContextSynchronizationMethod<T>.Post)
            {
                context.Post(new SendOrPostCallback(this.SendOrPostDelegate), new ContextEventHandlerArgsWrapper<T, T>(handler, sender, e));
            }
            else if (this._method == ContextSynchronizationMethod<T>.Send)
            {
               context.Send(new SendOrPostCallback(this.SendOrPostDelegate), new ContextEventHandlerArgsWrapper<T, T>(handler, sender, e));
            }
        }
    }
}
MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39
  • maybe decompile the class containing `ColorFrameReady` event and see what's going on? – Jake Berger Feb 27 '12 at 23:19
  • I have been in Reflector. I believe it has to do with how Microsoft Removes it's event handler. I can't tell for sure because I cant step through Microsoft dll without a pdb file – MyKuLLSKI Feb 28 '12 at 05:55
  • 1
    1. This link has some info on how to configure to use MS symbol server http://support.microsoft.com/kb/311503 2. Sometime it is possible to decompile the code with reflector than compile it again with VS. Then you'll have all the required symbols. 3. I would very carefully inspect my code to completely exclude the possibility of adding handler more than once (it happened to me); then removing the handler once does not stop the handler from execution. – user1234883 Feb 28 '12 at 07:00
  • My codes out of the question (as far as I know) I even tried this in the sample code the Microsoft provided. Ill look into the other 2 – MyKuLLSKI Feb 28 '12 at 16:03
  • 1
    Just to rule out the obvious, are you calling PauseKinect(false) multiple times and therefore hooking the event multiple times? – AndrewS Feb 28 '12 at 17:09
  • No offense but I have to say...Yea I'm not a moron. I can do the following any nothing happens... _Kinect.ColorFrameReady -= ColorFrameReadyEventHandler; _Kinect.ColorFrameReady -= ColorFrameReadyEventHandler; _Kinect.ColorFrameReady -= ColorFrameReadyEventHandler; _Kinect.ColorFrameReady -= ColorFrameReadyEventHandler; _Kinect.ColorFrameReady -= ColorFrameReadyEventHandler; _Kinect.ColorFrameReady -= ColorFrameReadyEventHandler; _Kinect.ColorFrameReady -= ColorFrameReadyEventHandler; – MyKuLLSKI Feb 28 '12 at 17:47
  • how does MS remove the event handler? – Jake Berger Feb 28 '12 at 21:03

1 Answers1

5

After posting an identical question on the Microsoft forum and talking to multiple Microsoft representatives they basically said the only way to do a "pause" is to enable/disable the streams (uncomment my comments). Without saying it straight forward its a bug in the SDK. They are going to talk to the people in the development team and try to fix the issue in future releases.

EDIT

In the May 2012 Release it is STILL not fixed. Thanks Microsoft!

Community
  • 1
  • 1
MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39