在事件驱动编程中一个接一个地调用函数 [英] Calling functions one after other in event driven programming

查看:62
本文介绍了在事件驱动编程中一个接一个地调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究软件为硬件面板发出命令的软件,一旦命令发出,几秒钟后就会收到响应。不同的硬件命令有不同的功能,例如

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();

一旦我们收到命令A的响应,我就会从响应中调用另一个函数,但这种方法并不符合设计模式的做法。



我想做的就是创建一个函数列表并一个接一个地调用所有这些函数,但是一旦我得到第一个函数的响应,就会调用下一个函数。



我尝试了什么:



我试过这个多播委托但我无法知道如果我将所有函数添加到该委托中,我们如何调用获取函数列表。这就是我正在尝试做的事情。

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.

What I have tried:

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 
                    }
                }
            }

推荐答案

查看Task类任务类(System.Threading.Tasks) [ ^ ]

和ContinueWith方法。



这就是你想要的:调用每个方法自己的任务(线程) - 等待方法执行而不阻止你的应用程序的其余部分。执行后继续下一个方法调用(在它自己的Task中)。另请参阅此模式的新语法支持(aync / await关键字)
have a look at the Task class Task Class (System.Threading.Tasks)[^]
and to the ContinueWith methods.

This is what you want: call methods each in it's own Task (thread) - wait for method to execute while not blocking the rest of your app. After executing continue with next method call (in it's own Task). Also see the new syntactic Support for this pattern (aync/await Keywords)


列出
Action

委派并添加状态每个功能的代码。假设已调用0均值函数,并且尚未调用1均值函数。然后使用LINQ Query来检查哪个函数已调用,哪个函数还剩。

Delegates and add Status Code for each function . let's say 0 mean function has invoked and 1 mean function is not invoked yet. then use LINQ Query to check which function has called and which one is remaining.
Change the status of function invocation from

OnReponse

方法更改函数调用的状态。

Method.


这篇关于在事件驱动编程中一个接一个地调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆