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!