如何知道dll事件何时被触发

Ela*_*ahe 2 c# dll events

我创建了一个包含此事件处理程序的DLL:

public void tcp1_Data(object sender, Sockets.DataEventArgs e)
{
  Tcp tcp = (Tcp)sender;

  response = "Socket Connection" + tcp.Tag.ToString() + " replied : " + e.Data.ToString();

  tcp.Close();
}
Run Code Online (Sandbox Code Playgroud)

当服务器在套接字连接中写入一些东西时,这将触发 所以,通过这个,我可以读取socket上的数据.

我在另一个项目中使用了这个dll.我想在我的项目(使用dll)中知道服务器在套接字连接上写入数据的确切时间.正如你在tcp1_Data事件中看到的那样,我将结果设置为响应变量,在主项目中(使用了dll),我检查了这个变量polling(如果响应不为null,则表示此事件被触发).但它不是我想要的.我不想一直检查这个变量.

还有其他方法吗?


我试过这个,因为@ThorstenDittmar说:

我的dll项目(名称为ClientSample)包含:

  1. TheClassInDLL Class

    public class TheClassInDLL
    {
    
        public event EventHandler<MyEventArgs> DataEventCalled;
    
        public void tcp1_Data(object sender, Sockets.DataEventArgs e)
        {
            Tcp tcp = (Tcp)sender;
    
            // Note: LOCAL variable
            string myresponse = "Socket Connection" + tcp.Tag.ToString() + " replied : " + e.Data.ToString();
    
            // Call the new event here
            if (DataEventCalled != null)
                DataEventCalled(this, new MyEventArgs(myresponse));
    
            tcp.Close();
        }
    
        // We use this class to pass arguments to the event handler
        public class MyEventArgs : EventArgs
        {
            public MyEventArgs(string response)
            {
                this.Response = response;
            }
    
            public string Response
            {
                get;
                private set;
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
    1. TCPSample class

    public class TCPSample {Tcp tcp = new Tcp(); tcp.Data + = new System.EventHandler

    在我上面使用的另一个项目中:

    public partial class Form1 : Form
    {
        private TheClassInDLL myClass;
        ClientSample.TCPSample t = new ClientSample.TCPSample();
    
        public Form1()
        {
            InitializeComponent();
    
            myClass = new TheClassInDLL();
            myClass.DataEventCalled += DataEvent;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            t.newTCP();
        }
    
        private void DataEvent(object sender, TheClassInDLL.MyEventArgs e)
        {
            MessageBox.Show(e.Response);
        }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

但它没有用,DataEvent从未发生过.谢谢你的帮助......

Tho*_*mar 5

你在这里写的是一个事件处理程序,当事情发生时调用.必须有一个包含此事件处理程序的类.声明并调用另一个事件而不是编写全局响应变量,您可以从该类外部订阅,如下所示:

public class <TheClassInDLL>
{
    public event EventHandler<MyEventArgs> DataEventCalled;

    // SNIP: All the other code that leads to tcp1_Data being called
    ...

    // The event handler that's called by some code in the class
    public void tcp1_Data(object sender, Dart.Sockets.DataEventArgs e)
    {
        Tcp tcp = (Tcp)sender;

        // Note: LOCAL variable
        string myresponse = "Socket Connection" + tcp.Tag.ToString() + " replied : " + e.Data.ToString();

        // Call the new event here
        if (DataEventCalled != null)
            DataEventCalled(this, new MyEventArgs(myresponse));

        tcp.Close();
    }

    // We use this class to pass arguments to the event handler
    public class MyEventArgs : EventArgs
    {
        public MyEventArgs(string response)
        {
            this.Response = response;
        }

        public string Response
        {
            get;
            private set;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

来自调用者,您可以像这样使用它:

public class <TheCallingClassOutsideDLL>
{
    private <TheClassInDLL> myClass;

    public TheCallingClassOutsideDLL()
    {
        myClass = new TheClassInDLL();
        myClass.DataEventCalled += DataEvent;
    }

    private void DataEvent(object sender, <TheClassInDLL>.MyEventArgs e)
    {
        Console.WriteLine(e.Response);
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,你需要更换<TheClassInDLL>,并<TheCallingClassOutsideDLL>与真实的类名!创建其他课程当然不起作用!