DanielV
DanielV

Reputation: 2670

Inject a class to subscribe to an inner event

I have the following:

public class Process
{
    private Action<Result> CaptureFinished = null;

    public Result StartProcess(Action<Result> onCaptureFinished)
    {
        CaptureFinished = onCaptureFinished;

        Console.WriteLine("Process invokes capture");
        CaptureFinished.Invoke(new Result("This is the capture"));

        return new Result("OK");
    }
}

So that this class is consumed by another process and captures something in a non linear way, the method OnCaptureFinished helps the consuming process to get the capture.

public class Consumer
{
    private Process _process;

    public Consumer(Process process)
    {
        _process = process;
        _process.StartProcess(Consume);
    }

    public void Consume(Result result)
    {
        Console.WriteLine("Consumer receives: {0}", result.ToString());
    }
}

I am forced to create another process that will get the capture delivered by "Process1" class, this other process is not able to consume the method StartProcess (like the class consumer above), but can control the creation of the "Process" class, therefore I was thinking on injecting another object that can be linked to the OnCaptureFinished method. Is it possible to do using an event handler?

Upvotes: 1

Views: 108

Answers (3)

DanielV
DanielV

Reputation: 2670

Based on @Juanito answer. I devised the following:

public class Sniffer
{
    public Action<Result> CaptureFinishedSniffed;

    public Sniffer()
    {
        CaptureFinishedSniffed = Captured;
    }

    public void Captured(Result result)
    {
        Console.WriteLine("Proper sniffer I have: {0}", result);
    }
}

then in the Process class:

public class Process
{
    private Action<Result> CaptureFinished = null;
    private Action<Result> CaptureSniffed = null;
    private Sniffer _sniffer;

    public Process(Sniffer sniffer)
    {
        _sniffer = sniffer;                             //1. INJECTION
        CaptureSniffed = sniffer.CaptureFinishedSniffed;//2.Plumbing with the delegate
    }

    public Result StartProcess(Action<Result> onCaptureFinished)
    {
        CaptureFinished = onCaptureFinished;
        ...
        CaptureFinished.Invoke(result);
        CaptureSniffed.Invoke(result);                  //3.Invoking
        return new Result("OK");
    }
}

As you can see 1.and 2. are providing the injection and required plumbing to connect the sniffer with its delegate inside the "Process" class. Step 3. is just invoking the desired method for further processing outside Process thread.

Upvotes: 0

Juanito
Juanito

Reputation: 420

If you inject another class (like sniffer):

public class Sniffer
{
    public Sniffer(){}

    public void ProcessResult(Result result)
    {
        Console.WriteLine("Sniffer I have: {0}", result);
    }
}

In your existing Process class:

public class Process
{
    private Action<Result> CaptureFinished = null;
    private Sniffer _sniffer;

    public Process(Sniffer sniffer)
    {
        _sniffer = sniffer;
    }

    public Result StartProcess(Action<Result> onCaptureFinished)
    {
        CaptureFinished = onCaptureFinished;
        ...
        CaptureFinished.Invoke(result);
        _sniffer.ProcessResult(result);

        return new Result("OK");
    }
}

You could get message in the sniffer class, when it is done, and invoke it from the main thread:

_sniffer = new Sniffer();
_process = new Process(_sniffer);

Is this what you are looking for?

Upvotes: 1

keith
keith

Reputation: 5332

You can use .NET remoting for inter-process communication. You can use .NET events over .NET remoting.

Here's an article on the subject: https://www.codeproject.com/Articles/62813/NET-Remoting-Events-Explained

You may want to explore asynchronous communication as well.

If you are just running a separate app domain in the same process you can use MarshalByRefObject to proxy calls in/out of the app domain.

Upvotes: 2

Related Questions