如何跨不同类发布和订阅事件

时间:2013-03-17 00:36:56

标签: c# events delegates

目标:当udp或tcp使用其send方法时更改表单上的图像

问题:我不知道如何正确设置事件,事件处理程序和委托

发送界面

interface ISendData
{
  void Send();
}

Tcp连接类

//Need some type of delegate??

public class TCPconnection : ISendData
{
   void Send()
   {
     //how invoke/fire a send Event?
   }
}

UDP连接类

//Need some type of delegate??

public class UDPConnection : ISendData
{
   void Send()
   {
     //how invoke/fire a send event?
   }
}

'应该'订阅看到被解雇的事件的winform

public class myForm
{
   private DataWatcher datawatcher = new DataWatcher();
   private Image statusIndicator = null;

   public myform()
   {
     initComponents();

     datawatcher.DataSendActive += new DataWatcherSendHandler(DataSending);
     datawatcher.DataSendInactive += new DataWatcherSendHandler(NoDataSending);
   }

   public void DataSending(object sender, DataWatcherArgs e)
   {
      statusIndicator = Properties.resources.greenLight;
   }

   public void NoDataSending(object sender, DataWatcherArgs e)
   {
      statusIndicator = Properties.resources.redLight;
   }

}

事件/事件处理程序??但我真的不知道我在这里做了什么来做这项工作

 public delegate void EventHandler(object sender, EventArgs e);

    class DataWatcher
    {
        public event EventHandler DataSendActive;
        public event EventHandler DataSendInactive;

        protected virtual void onDataSendActive(System.EventArgs e)
        {
            if (DataSendActive != null)
            {
                DataSendActive(this, e);
            }
        }
        protected virtual void onDataSendInactive(System.EventArgs e)
        {
            if (DataSendInactive != null)
            {
                DataSendInactive(this, e);
            }
        }
     }

2 个答案:

答案 0 :(得分:3)

这是一个简单的示例,说明如何实现发送事件而不发送和订阅事件

public class Connection
{
    //Set up an event
    public event EventHandler DataSending;
    public event EventHandler DataNotSending

    //This method will trigger the event for sending
    private void OnDataSending()
    {
        if (DataSending!= null) { DataSending(this, EventArgs.Empty); }
    }

    //this method will trigger the event for finished sending
    private void OnDataNotSending()
    {
        if (DataNotSending!= null) { DataNotSending(this, EventArgs.Empty); }
    }

    //This method performs your send logic
    public void Send()
    {
         //Call your method that tells the event to be raised
         OnDataSending();
         //Then put your send code
         OnDataNotSending(); //we're done!

    }
}

这是您在消费程序中使用它的方式

public class myForm
{
     //This method is the one that sets up the 
     //instance and subscribes to the event
     public myForm()
     {
         Connection con = new Connection();

         con.DataSending += new EventHandler(con_DataSending);
         con.DataNotSending += new EventHander(con_DataNotSending);

     }

     void con_DataSending(object sender, EventArgs e)
     {
         //Put your subscription logic here.
         //Whatever you want to do in response to a send
     }

     void con_DataNotSending(object sender, EventArgs e)
     {
         //Put your subscription logic here.
         //Respond to it not sending
     }
}

答案 1 :(得分:3)

有许多惯例用于执行此操作。这是我的小实现。

public enum ActivityState
{
    Sending,
    Receiving,
    Idle
}

public interface IDataTransferManager
{
    // This event will fire when the activity state changes.
    // note that Action<T> is introduced in .NET 3.5
    // if you're using .NET 2.0, you can use a delegate.
    event Action<ActivityState> DataActivityStateChange;


    void Send(byte[] data);
    //byte[] Receive(); 
    // ... more methods ... //

}

现在TcpConnection类将实现它。

public class TcpConnection : IDataTransferManager
{
    public event Action<ActivityState> DataActivityStateChange;

    public void Send(byte[] data)
    {
        // we're sending data. fire the change event
        FireDataActivityStateChange(ActivityState.Sending);

        //TODO: send the data

        // we're done sending. Fire the change event
        FireDataActivityStateChange(ActivityState.Idle);
    }



    private void FireDataActivityStateChange(ActivityState state)
    {
        // helper method, so I don't have to check the event 
        // to avoid null reference exceptions.
        if (DataActivityStateChange != null)
            DataActivityStateChange(state);
    }

}

以下是表单的设置。

class MyForm // :Form
{
    IDataTransferManager dataManager;

    public MyForm()
    {   // here, usually an instance will be passed in, 
        // so there's only one instance throughout the application.
        // let's new up an instance for explanation purposes.
        dataManager = new TcpConnection();

        dataManager.DataActivityStateChange += (state) => 
        {
            // NOTE: if you don't like inline, 
            // you can point this labda to a method.

            switch (state)
            {
                case ActivityState.Sending:
                    // change the image to the spinning toilet ball
                    break;
                case ActivityState.Receiving:
                    // change the image to the spinning toilet ball, but reverse :P
                    break;
                case ActivityState.Idle:
                    // hide it ?
                    break;
            }
        };
    }
}

希望这有帮助。