帮助与代表一起解释代码

问题描述

| 我是C#和我正在使用的框架的新手,我正在尝试弄清楚某些代码是如何工作的(代码没有错)。它是一个客户端/服务器应用程序,它将客户端的一些文本发送到服务器,然后在文本框中接收并显示相同的字符串。 下面的代码来自客户端及其形式。这里仅包括用于从服务器接收字符串的内容。我包括了一些来自框架的评论
public class TestModuleMobile : PreCom.Core.ModuleBase,PreCom.Core.IForm
{
    public delegate void ReceiveDelegate(string data);
    public event ReceiveDelegate DataReceived;

    public void Receive(byte[] data)
    {
        string text = Encoding.UTF8.GetString(data,data.Length);

        if (DataReceived != null)
            DataReceived.Invoke(text);
    }

    public override bool Initialize()
    {
        PreCom.Application.Instance.Communication.Register(99,Receive);         
    // Register(uint receiverID,RecieveDelegate receiver): Called by modules to register for communication.
    //
    //      Parameters: 
    //          receiverID:
    //              Module Id
    //          receiver:
    //              The module receive function that will be called by the framework when data
    //              arrives to specific module. (This method should return as soon as possible
    //              to avoid timeouts)
        _isInitialized = true;
        return true;
    }
}

public partial class TestModuleMobileForm : PreCom.Controls.PreComForm
{
    TestModuleMobile _module;

    public TestModuleMobileForm(TestModuleMobile module)
    {
        _module = module;
        _module.DataReceived += new TestModuleMobile.ReceiveDelegate(DataReceived);
        InitializeComponent();
    }

    void DataReceived(string data)
    {
        if (Invokerequired)
        {
            ThreadStart myMethod = delegate { DataReceived(data); };
            this.BeginInvoke(myMethod);
            return;
        }  
        listBox1.Items.Insert(0,data);
        this.preComInput21.Text = \"\";
    }
}
问题:  1.公共重写bool Initialize() 对Register的函数调用将ReceiveDelegate对象作为第二个参数。那么,当它只是一个函数时,如何向它发送一个函数(接收)呢?这是如何运作的?  2. public void Receive(byte [] data) 如果发生这种情况会怎样?调用如何工作?  3. void DataReceived(字符串数据) 在if情况下(逐行)会发生什么?     

解决方法

在Stackoverflow上有许多相关的帖子,您可以浏览以更好地了解委托。阅读完它们后,请重新看一下代码,您会发现它更易于理解。 提示:查看此网页的右侧以查看所有相关文章。     ,您需要充分了解代表,因此最好从以下顺序开始阅读: 代表(C#编程指南) 代表教程 C#/ .NET中的代表和事件     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...