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

时间:2014-07-22 07:28:28

标签: c# events dll

我创建了一个包含此事件处理程序的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();
}

当服务器在套接字连接中写入某些内容时,这将触发。所以,我可以读取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;
            }
        }
    }
    
    1. TCPSample class
    2. 公共类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);
          }
      
      }
      

      但它没有用,DataEvent永远不会发生。 谢谢你的帮助...

2 个答案:

答案 0 :(得分: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;
        }
    }
}

来自来电者,你可以这样使用它:

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

当然,您需要将<TheClassInDLL><TheCallingClassOutsideDLL>替换为真实的班级名称!创建其他课程当然不起作用!

答案 1 :(得分:0)

为此,您必须定义自己的事件并在需要时提升它... 例如 - &gt;在您设置“响应”变量的类中定义一个事件

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

也许你还需要CustomEventArgs(在上面的例子中使用)

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

}

使用dll并希望收到通知的类需要注册此事件

YourDllClassInstance.MyCustomEvent += OnMyCustomEvent;

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

这意味着在您的dll课程中,当您想要举起活动时,您必须执行以下操作

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

这就是你要求的吗?