我应该使用活动吗? [英] Should I Use Events?

查看:105
本文介绍了我应该使用活动吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家,我需要一个建议,我正在开发一个使用C#的桌面应用程序(Winform),我有一个表单,我有一个分离的类名为OPC(其他文件)



这是OPC类的一些代码



OPC类

 命名空间BarCodePrint.Class 
{
public class OPC
{
public OPCServer ConnectedOPCServer {get;组; }
public OPCGroups ObjOPCGroup {get; set;}
public OPCGroup ConnectedOPCGroup {get;组; }
public int _numItems {get;组; }
public string _nodeName {get;组; }

数组_OPCItemIDs;
数组_ItemServerHandles;
数组_ItemServerErrors;
数组_ClientHandles;
数组_RequestedDataTypes;
数组_AccessPaths;
数组_WriteItems;

public OPC()
{
//代码
}

public void OPCConnect()
{
//代码
}

public void OPCDisconnect()
{
//代码
}

public void AddGroupToOPC ()
{
//代码
}

public void AddItemsToOPC()
{
//代码
}

public void Write()
{
//代码
}


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


if((Convert.ToInt32(ClientHandles.GetValue(i))== 1))
{
//做某事
}
}
}
catch(Exception exc)
{
// HandleException
}

}

}
}

方法 ConnectedOPCGroup_DataChange 每次在设备中更改值时执行,我想做什么是当一个值更改将该值发送到一个表单时,问题是:我应该使用事件吗?或者将OPC类放在Form的同一个文件中?或者我可以这样做吗?



我希望能够很好地解释我。



谢谢

解决方案

如果您询问是使用事件还是直接调用某些东西,那取决于您的用例。活动是广播动作的好方法。如果有多个项目需要知道使用事件改变的内容,可以将代码解耦。也就是说,如果只关心另一个代码,那么你也可以这样做。事件在相同的线程上调度,所以无论您是直接调用用户还是使用事件进行调度,执行代码在相同的上下文中处理。



我个人喜欢使用事件来防止硬耦合。您的调度员代码不需要知道谁在订阅。它只是说嘿,事情发生了,现在你做工作。



事件也很整洁,因为您可以将其与Rx结合起来进行抽样和调节,并进行各种其他酷炫的事情。



如果你发现你正在添加一堆额外的代码来传播发生了什么事情,你应该使用一个事件。


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)

This is some code of OPC Class

OPC Class

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? or put my OPC Class in the same file of the 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.

If you find you are adding a bunch of extra code to propagate that "something happened", you should use an event.

这篇关于我应该使用活动吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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