数据到达列表时的调用方法 [英] Invoking Method when data arrives in List

查看:58
本文介绍了数据到达列表时的调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数据消息到达类列表时,有人知道如何从类内调用方法吗?
例如,假设我们有一个列表,可以接收法国,德国和意大利的数据消息.第一条消息到达列表后,将调用一个类方法,例如将所有数据消息打印到控制台.一旦列表为空,该方法结束并等待直到再次调用它.
我不想在该方法中使用Sleep().数据来自应用程序中的另一个组件.
谢谢您预先提供的帮助.

Does anyone know how to call a method from within a class when a data message arrives in the class List?
For example, say we have a List, that receives data messages, France, Germany and Italy. As soon as the first message arrives in the List, a class method is called, say to print all the data messages to the console. Once the List is empty, the method ends and waits until it is called again.
I do not want to use a Sleep() iin the method. The data arrives from another component in the application.
Thank you for any help in advance.

推荐答案

我不确定您想做什么,但听起来是最好的选择.是包装List类的方法以执行所需的逻辑.

换句话说,创建一个新类并创建一个内部List类,并通过调用内部List来实现所需的必要方法.

--------------------------------

静态操作不会阻止多次执行,仅表示其不与类的特定实例绑定.您将要阅读 [
I''m not 100% clear on what you''re wanting to do, but it sounds like your best bet is to wrap the methods of the List class to perform the logic you need.

In other words, create a new class and create an internal List class and implement the necessary methods you need by calling the internal List.

--------------------------------

Static does not prevent multiple executions, just means its not tied to a specific instance of a class. You''ll want to read this[^].

You''ll want to execute a method on the threadpool, but just have a critical section around a flag indicating that it is executing. You''ll then check that every time you get a new message, if its not set execute the thread, otherwise sit tight and let the thread handle it.

Just make sure that if your thread fails or throws an exception that you unset that flag, so it won''t clog up your system. Take care.


这篇关于数据到达列表时的调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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