简体   繁体   English

如何知道DLL事件何时触发

[英]How to know when dll events fired

I created a dll contain this event handler: 我创建了一个包含此事件处理程序的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();
}

this will fire when server write some thing in socket connection. 当服务器在套接字连接中写一些东西时,这将触发。 so by this, I can read the data on socket. 因此,我可以读取套接字上的数据。

I used this dll in another project. 我在另一个项目中使用了该dll。 I want to know in my project (that used dll) exactly when server is writing data on socket connection. 我想在我的项目(使用dll)中确切地知道服务器何时在套接字连接上写入数据。 as you see in tcp1_Data event, I set result into response variable and in main project (that used dll), I checked this variable polling (if response is not null, it means that this event fired). 正如您在tcp1_Data事件中看到的那样,我将结果设置为响应变量,在主项目(使用dll)中,我检查了此变量轮询(如果response不为null,则表示触发了此事件)。 but Its not what I want. 但它不是我想要的。 I dont want check this variable all the time. 我不想一直检查此变量。

is there any other way? 还有其他办法吗?


I tried this as @ThorstenDittmar said: 我尝试了这个,就像@ThorstenDittmar所说的:

my dll project (its name is ClientSample) contain: 我的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; } } } 
    1. TCPSample class

    public class TCPSample { Tcp tcp = new Tcp(); 公共类TCPSample {Tcp tcp = new Tcp(); tcp.Data += new System.EventHandler tcp.Data + =新的System.EventHandler

    and in another project that I used above dll: 以及我在dll上使用的另一个项目中:

     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); } } 

but It didnt work, DataEvent never happend. 但是它没有用,DataEvent从未发生。 Thanks for any helping... 感谢您的帮助...

What you wrote here is an event handler that is called when something happens. 您在此处编写的是一个事件处理程序,该事件处理程序在发生某些情况时会被调用。 There must be a class containing this event handler. 必须有一个包含此事件处理程序的类。 Instead of writing a global response variable, declare and invoke another event you can subscribe to from outside that class like this: 无需编写全局响应变量,而是声明并调用另一个事件,您可以从该类外部进行订阅,如下所示:

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;
        }
    }
}

From the caller, you use it like this: 在呼叫者中,您可以像这样使用它:

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);
    }
}

Of course you need to replace <TheClassInDLL> and <TheCallingClassOutsideDLL> with the real class names! 当然,您需要用真实的类名替换<TheClassInDLL><TheCallingClassOutsideDLL> Creating additional classes of course doesn't work! 创建其他课程当然是行不通的!

For that you got to define your own event and raise it when needed... for Example -> In the class where you set the "response" variable define an event 为此,您必须定义自己的事件并在需要时引发它。例如->在设置“响应”变量的类中定义一个事件

//your custom event
public event EventHandler<CustomEventArgs> MyCustomEvent;

//This will raise your event and notify all who registered
private void RaiseMyCustomEvent(CustomEventArgs e)
{
    var handler = MyCustomEvent;
    if (handler != null)
    {
        handler(this, e);
    }
}

Maybe you will also need CustomEventArgs (used in the example above) 也许您还需要CustomEventArgs(在上面的示例中使用)

public class CustomEventArgs : EventArgs
{
    public String Message {get;private set;}
    public CustomEventArgs(String message){
        this.Message = message;
    }

}

The class which is using the dll and that wants to get notified needs to register for this event 正在使用dll并希望获得通知的类需要注册此事件

YourDllClassInstance.MyCustomEvent += OnMyCustomEvent;

public void OnMyCustomEvent(object sender, CustomEventArgs e){
    Console.WriteLine("Event received");
}

That means in your dll class you got to do something like the following when you want to raise the event 这意味着在您的dll类中,当您想引发该事件时,必须执行以下操作

response = "blablabla";
RaiseMyCustomEvent(new CustomEventArgs(response);

Is that what you where asking for? 那是您要的东西吗?

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

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