0

I'm working on a software where software issues commands for hardware panel and once a command is issued, its response received after few seconds . there are different functions for different hardware commands like

public void FunctionA()
{
    StartCommandA();
}

and other functions on the same pattern that will be used to run other commands.

FunctionB();
FunctionC();

Once we receive the response of command A , I invoke the other function from the response but this approach is not good as per design pattern practices.

All i want to do is to make a list of functions and invoke all these functions one after other, But next function will be called once i get response of first functions.

I tried this by using Multicast delegate but I'm unable to find out how we can call get the list of functions once i add all functions to that delegates. This is what i'm trying do since.

FunList funList_ConfigAndSerialTests = new FunList(StartSerialTest);

        funList_ConfigAndSerialTests += StartSerialTest;

        funList_ConfigAndSerialTests += StartMsrTest;

        funList_ConfigAndSerialTests += StartContactLessTest;

        //funList_ConfigAndSerialTests.Invoke();

        Delegate[] del = funList_ConfigAndSerialTests.GetInvocationList();

        foreach (Delegate item in funList_ConfigAndSerialTests.GetInvocationList())
        {
            while (true)
            {
                if (IsResponseReceived == true)
                {
                    // Call function here 
                }
            }
        }

2 Answers2

2

The simplest way to do this is to call the functions one by one:

FunctionA();
FunctionB();
FunctionC();

Each method will be called only after the previous has returned.

But you said you want to call the next function after the previous one has a response. Now that sounds like your functions run asynchronously. I strongly suggest you use the async keyword to mark your functions and make them return a Task<ResonseType>. You can learn about this here.

You'll then be able to do something like this:

await FunctionA(); // you obviously want to do something with the returned response
                   // I do not know your requirements so I did not show that part
await FunctionB();
await FunctionC();
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Yes all functions are running asynchronously and We are calling the next function once we get response of first functions . Note : response of functions is being received in a separate function name like "OnResponse" –  Jan 24 '18 at 07:15
  • @J.Doe Well if you have an `OnResponse` function then call the next function there! – Sweeper Jan 24 '18 at 07:22
  • This is the problem, I don't want to call the next function from OnResponse method. –  Jan 24 '18 at 07:29
  • List of `Func` to await seems exactly what OP need to implement. – Alexei Levenkov Jan 24 '18 at 07:31
  • This will work fine and seems like the exact solution i was looking for @Alexei . –  Jan 24 '18 at 07:35
1

It seems what you're trying to achieve is what Events are for. In the class, where the handlers (FunctionA, FunctionB, ...) are defined create an event instance as follows:

public class MyClass
{
    private event Action Event;

    public void RegisterHandlers()
    {
        Event += FuncA;
        Event += FuncB;

        Event();
    }

    public void HandleCommand()
    {
        this.Event();
    }

    private void FuncA() { /*...*/ }
    private void FuncB() { /*...*/ }
}

The simple call to Events() will actually result in all the registered handlers to be invoked in the order they've been registered.

Artak
  • 2,819
  • 20
  • 31
  • Well I've already made a method named "OnResponse" and I'm handling all the response in that method. –  Jan 24 '18 at 07:32
  • The code I included was just a sample. I would assume you'd rename and modify things to match your case. The main point there was the usage of an 'event' instance. – Artak Jan 24 '18 at 07:34