0

I´m working on a Kinect project. I'm recording data from the received frames. When I have to process the frame data, the program is taking a lot of time. In the meanwhile, I want to disable the EventHandler MultiSourceFrameArrived.

The thing is, I have been reading different posts, I can't find an answer that fits my problem. The expression -= seems to work only if it is in the same scope as the expression +=. When I write them in different scopes, the frames are still arriving and I can't disable that event handler.

The NON WORKING code is this one:

private void MainWindow()
{
    //Intitalize components
    if (this.multiSourceFrameReader != null)
    {
        EnableFrameArrived();        
    }
}

private void MultiSourceFrameReader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
{
     DisableFrameArrived();
}

private void DisableFrameArrived()
{
    this.multiSourceFrameReader.MultiSourceFrameArrived -= this.MultiSourceFrameReader_MultiSourceFrameArrived;
//This doesn`t cancel my suscription to the event.
}

private void EnableFrameArrived()
{
    this.multiSourceFrameReader.MultiSourceFrameArrived += MultiSourceFrameReader_MultiSourceFrameArrived;
}

The WORKING code is this one:

private void MainWindow()
{
    //Intitalize components
    if (this.multiSourceFrameReader != null)
    {
        this.multiSourceFrameReader.MultiSourceFrameArrived += this.MultiSourceFrameReader_MultiSourceFrameArrived; 
        //I subscribe to the event
    }
    this.multiSourceFrameReader.MultiSourceFrameArrived -= this.MultiSourceFrameReader_MultiSourceFrameArrived;
    //I cancel my subscription. But I need to cancel my subscription in another scope. 
}

I commented the rest of the code and just left this part running. I keep having incomings frames even though the Disable Method is reached. Why the cancellation to my event is only possible if I'm in the same scope? I`ve reading different blogs but I can't solve this.

Any suggestions? Thanks!

  • How many times is `EnableFrameArrived()` called? That `&& flag` thing is not thread safe (although it probably should run on the UI thread only). – Thomas Weller Feb 11 '20 at 20:11
  • Only one time in this code. In `MainWindows_Loaded()`. My primary idea is to call it again when I finish the processing, but I can't make DisableFrameArrived() work since the frames are still arriving. I asked about the flag to check if it is the first time it is been called. – Magali Sganga Feb 11 '20 at 20:14
  • Well, [the loaded event can fire more than once](https://stackoverflow.com/questions/3421303/loaded-event-of-a-wpf-user-control-fires-more-than-once) – Thomas Weller Feb 11 '20 at 20:18
  • Thanks Thomas for your answer. I checked what you meant, and I changed the code. I put the `EnableFrameArrived()` in the `MainWindow()` function. When `this.multiSourceFrameReader.MultiSourceFrameArrived += this.MultiSourceFrameReader_MultiSourceFrameArrived` is in the same scope as `this.multiSourceFrameReader.MultiSourceFrameArrived -= this.MultiSourceFrameReader_MultiSourceFrameArrived;` they work just fine. But, when they are not in the same scope, the Disable Method `-=` is not working. I think is a problem with the lambda call, but I don`t know how to solve it. – Magali Sganga Feb 11 '20 at 21:00
  • You say that the problem is "with the lambda call" but there are no lambdas in your program fragment. **If your event handlers are lambdas instead of method groups then indeed you can run into this problem**. Can you show the actual code that has the problem? – Eric Lippert Feb 11 '20 at 22:04
  • Eric, as you say, I have no lambda calls, I correct my question when I realized that. I will edit the question with the code that actually works. – Magali Sganga Feb 11 '20 at 22:25

0 Answers0