I have a Kinect device and I am developing a program with it by using C#.
In order to manage the device, I have used AllFramesReady
event in order to process the depth and color information.
I have created an event handler to process the data which is named EventHandler1
. I am doing a lot of processing inside this event handler.
I would like to do some more computation inside a second event handler named EventHandler2
.
Is it possible to run this 2 event handlers which are basically 2 functions on parallel, on 2 different threads of the main process? If possible, could you please give me a sample code for doing this?
This is easy enough to wrap up in a class; however, you need to aggregate all the event handlers into a single event handler before subscribing to the desired event.
Here is a quick-and-dirty class to demonstrate this. The first event provided runs inline with the event call while all others are executed on the default thread pool.
Now to use this we construct a ParallelEvent class providing it all the event handlers we want to run in parallel. Then we subscribe to the event 'test' with the class's Handler method. Finally we call the event 'test' and review the output. Consider the following example:
As expected the program above runs them in parallel as demonstrated by the following output:
Lastly you need to be aware of other concerns regarding multi-threaded code. Any shared state being changed now needs to be synchronized, etc.
With a little work you could adapt the above class to expose an event so that listeners can subscribe and unsubscribe at will. Then in the Handler method you would extract the delegate list via Delgate.GetInvocationList(). Once you have a list of delegates you can process them the same as the existing Handler method above.