0

In my application I have a interface IEncoder that is having event EncoderCaller.

public interface IEncoder
{
    event EncoderCaller EncoderCalled;
}

public  delegate void EncoderCaller(object Source, EventArgs args);

public class Video
{
    public string Title { get; set; }
}

public class VideoEventArgs : EventArgs
{
    public Video xVideo { get; set; }
}


public class DetectionAction : IEncoder
{
    public event EncoderCaller EncoderCalled;

    public void Encode(Video video)
    {
        //some logic to encode video

        OnVideoEncoded();
    }

    protected virtual void OnVideoEncoded()
    {
        if (EncoderCalled != null)
            EncoderCalled(this, EventArgs.Empty);

    }
}

public class Client1: IEncoder
{

}

I need some mechanism by which I should be able to share a contract, if that is implemented by any client then that event will trigger event in my class DetectionAction .

Can someone tell me, Am I doing right thing.

How it can be done?

AMIT SHELKE
  • 501
  • 3
  • 12
  • 34
  • 1
    I don't get you question. Would you be able to clarify what needs to be done with some more examples? – weichch Mar 22 '20 at 09:23
  • Let say I have two application, Application A and Application B. I want to share a contract which has event notification kind of thing, where if application B implement that event and on that event application A event should also get raised. This is something I am trying to achieve – AMIT SHELKE Mar 22 '20 at 12:35

1 Answers1

0

If you have two classes in the same process, you could consider explicitly chain events like this:

public class Client1 : IEncoder
{
    public event EncoderCaller EncoderCalled;

    public Client1(IEncoder anotherEncoder)
    {
        // Listen to event raised on another instance and raise event on this instance.
        anotherEncoder.EncoderCalled += OnAnotherEncoderCalled;
    }

    private void OnAnotherEncoderCalled(object source, EventArgs args)
    {
        if (EncoderCalled != null)
            EncoderCalled(this, EventArgs.Empty);
    }
}

In this case, for example, anotherEncoder is DetectionAction.

However, if you are seeking solution for sharing events between two different applications running in different processes, you might be looking at inter-process communication, like this post:

Listen for events in another application

And the above example code still works, but the IEncoder in this case is an implementation with IPC support, for example a message queue listener which raises the event on message received.

weichch
  • 9,306
  • 1
  • 13
  • 25