3

I use a third party library for speech recognition.

This library give me access to some classes ( SpeechRecognitionEngine, SpeechRecognitionConfiguration etc) which give me access to some events (OnSpeechRecognized, OnSpeechRecognitionStarted etc).

So as I could want to reuse this library any day, I created a project dedicated to handle it and give me access to only what I want.

My custom project main class has an instance of this third party engine :

private ThirdPartyASREngineControl _engine;

Which gives me access to the event RecognitionStarting this way within my custom project:

  _engine.RecognitionStarting += /*SOME BEHAVIOUR*/

The signature of RecognitionStarting and _ThirdPartyEngineControlEvents_RecognitionStartingEventHandler are :

event _ThirdPartyEngineControlEvents_RecognitionStartingEventHandler RecognitionStarting;

public delegate void _ThirdPartyEngineControlEvents_RecognitionStartingEventHandler(); 

Now my concern is that I want to be able to replace this some behaviour with anything I want in any projects using MySpeechRecognitionWrapperProject.

Here is what I've come with so far :

using ThirdPartyASR;
using System;
namespace MySpeechRecognitionWrapperProject
{
    public class MyWonderfulMainClass
    {

        private _ThirdPartyEngineControlEvents_RecognitionStartingEventHandler _recognitionStartedAction;
        public Action RecognitionStartedAction
        {
            set
            {
                if (_recognitionStartedAction != null)
                {
                    _engine.RecognitionStarting -= _recognitionStartedAction; // i don't believe this will work
                }
                _recognitionStartedAction =()=> value();
                _engine.RecognitionStarting += _recognitionStartedAction;
            }
        }

        private ThirdPartyASREngineControl _engine;

        public MyWonderfulMainClass()
        {
            _engine = new ThirdPartyASREngineControl();
            /*
             * more code
             *
             */
        }
        /*
         * even more code
         *
         */
    }
}

As you can see my goal here is that I can set any void behaviour that I want from any project using MySpeechRecognitionWrapperProject.

As someone with great difficulties wrapping my head around the event system in C#, I'm almost certain that what I'm doing is far from the neatest solution one would come with in this situation.

Is there a better way?

4
  • 1
    Except for the lambdas this doesn't look all that different from any other event subscriber code I've seen. – Robert Harvey Nov 23 '16 at 14:48
  • 3
    As far as i know you cannot unsubscribe from an event when using lambda expressions, _engine.RecognitionStarting -= () => _recognitionStartedAction() is a different lambda expression even if they call the same function – Dtex Nov 23 '16 at 15:35
  • I agree with you. I edited the question with a better solution. Thanks ! – yan yankelevich Nov 23 '16 at 15:39
  • @Dtex You can unsub a lambda, but you have to hold onto a reference to the lambda to do so. Creating a new lambda, even if it has the same code, isn't going to be equal. – Servy Jan 23 '17 at 16:56
-1

Yes. An EventHandler has caller and event parameters which an Action has not.

Why not expose an EventHandler<T> or at least an Action<T>

By using Actions you are reimplementing the EventHandler system

3
  • In this situtation i cannot use an EventHandler as my event RecognitionStarting is of type _ThirdPartyEngineControlEvents_RecognitionStartingEventHandler which is basically an Action as it is a delegate void foo(); – yan yankelevich Nov 23 '16 at 16:12
  • if you are hiding the underlying implementation the type could be mapped – Ewan Nov 23 '16 at 16:14
  • I don't get it. Could you please give me an example ? – yan yankelevich Nov 23 '16 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.