简体   繁体   English

我应该使用活动吗?

[英]Should I Use Events?

Hi everyone I need a advice, I'm developing a desktop application (Winform) using C#, I have a Form and I have a separated class named OPC (other file) 大家好我需要一个建议,我正在使用C#开发一个桌面应用程序(Winform),我有一个Form,我有一个名为OPC的分离类(其他文件)

This is some code of OPC Class 这是OPC Class的一些代码

OPC Class OPC类

namespace BarCodePrint.Class
{   
public class OPC
{
    public OPCServer ConnectedOPCServer { get; set; }
    public OPCGroups ObjOPCGroup {get; set;}
    public OPCGroup ConnectedOPCGroup { get; set; }
    public int _numItems { get; set; }
    public string _nodeName { get; set; }

    Array _OPCItemIDs;
    Array _ItemServerHandles;
    Array _ItemServerErrors;
    Array _ClientHandles;
    Array _RequestedDataTypes;
    Array _AccessPaths;
    Array _WriteItems;

    public OPC()
    {
        //Code
    }

    public void OPCConnect()
    {
       //Code
    }

    public void OPCDisconnect()
    {
        //Code
    }

    public void AddGroupToOPC()
    {
       //Code
    }

    public void AddItemsToOPC()
    {
        //Code
    }

    public void Write()
    { 
        //Code
    }


    private void ConnectedOPCGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
    {
        try
        {
            for (int i = 1; i <= NumItems; i++)
            {


                if ((Convert.ToInt32(ClientHandles.GetValue(i)) == 1))
                {
                   //Do something
                }
            }
        }
        catch (Exception exc)
        {
          //HandleException
        }

    }

}
}

The method ConnectedOPCGroup_DataChange execute everytime that a value changes in the device, what I would like to do is when a value changes send that value to a Form, the question is: Should I use events? 方法ConnectedOPCGroup_DataChange每次在设备中更改值时执行,我想要做的是当值更改将该值发送到Form时,问题是:我应该使用事件吗? or put my OPC Class in the same file of the Form? 或者将我的OPC类放在Form的同一个文件中? or can I do it in other way?. 或者我可以用其他方式吗?

I hope explained me well. 我希望能很好地解释我。

Thanks 谢谢

If you are asking whether to use events or whether to just directly call into something, it depends on your use case. 如果您询问是否使用事件或是否直接调用某些内容,则取决于您的用例。 Events are a great way to broadcast an action. 事件是广播动作的好方法。 If more than one item needs to know about something changing using events can decouple your code. 如果有多个项目需要了解使用事件更改的内容,则可以解耦代码。 That said, if you only care about telling one other piece of code then you can do that too. 也就是说,如果您只关心另一段代码,那么您也可以这样做。 Events are dispatched on the same thread, so whether you directly call into the subscriber or dispatch it with an event, the executing code is handled in the same context. 事件在同一个线程上调度,因此无论您是直接调用订阅者还是使用事件调度它,执行代码都在相同的上下文中处理。

Personally I like to use events to prevent hard coupling. 我个人喜欢使用事件来防止硬耦合。 Your dispatcher code doesn't need to know who is subscribing on it. 您的调度程序代码无需知道订阅者是谁。 It just says "hey, stuff happened, now you do work". 它只是说“嘿,事情发生了,现在你做了”。

Events are also neat because you can combine them with Rx to get sampling and throttling and do all sorts of other cool stuff. 事件也很整洁,因为你可以将它们与Rx结合起来进行采样和限制,并做各种其他很酷的事情。

If you find you are adding a bunch of extra code to propagate that "something happened", you should use an event. 如果您发现要添加一堆额外的代码来传播“发生的事情”,那么您应该使用一个事件。

There are two ways to get data from an OPC server: read directly or use the DataChange event. 从OPC服务器获取数据有两种方法:直接读取或使用DataChange事件。 the choice is up to you and one or the other cannot be described as "best practice". 选择取决于你,一个或另一个不能被描述为“最佳实践”。 If your question is just about implementation then I can tell you that you're missing " Handles ConnectedGroup.DataChange " on the end of your ConnectedOPCGroup_DataChange sub. 如果您的问题只是关于实现,那么我可以告诉您,您在ConnectedOPCGroup_DataChange子的末尾缺少“Handles ConnectedGroup.DataChange”。 Your OPCAutomation library will generate the event, you just have to handle it. 您的OPCAutomation库将生成事件,您只需处理它。

To address the second part of your question, it would be best practice to have your OPC client class separate from your main form. 要解决问题的第二部分,最好将OPC客户端类与主表单分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM