我试图通过开发一个通用的弱事件侦听器来节省时间,我可以只传递一个 Action 来采取。在未注册之前,一切似乎都很好。这似乎将它们全部注销。我感到困惑的是为什么,这与将其传递给IWeakEventListener 参数有何不同?
public class GenericWeakEventListener : IWeakEventListener
{
#region EventAction
/// <summary>
/// Action to take for the event
/// </summary>
private Action<Type, object, EventArgs> _eventAction;
/// <summary>
/// Gets or sets the action to take for the event
/// </summary>
//[DataMember]
public Action<Type, object, EventArgs> EventAction
{
get
{
return _eventAction;
}
private set
{
if (EventAction != value)
{
_eventAction = value;
}
}
}
#endregion EventAction
#region Constructors
public GenericWeakEventListener(Action<Type, object, EventArgs> action)
{
EventAction = action;
}
#endregion Constructors
#region Public Methods
public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
if (EventAction != null)
{
EventAction(managerType, sender, e);
}
return true;
}
#endregion Public Methods
}
编辑:
这是监听器代码:
public class SomeClient
{
public int ID { get; set; }
private Timer timer = null;
private Timer timer2 = null;
public SomeClient(int id, SomeService service)
{
ID = id;
//EventHandler<GenericEventArgs<string>> d = (o, s) => Console.WriteLine("Client {0}: {1}", ID, s.Item);
if (service != null) SomeEventChangedEventManager.AddListener(service, new GenericWeakEventListener((t, s, e) => { Console.WriteLine("SomeEvent: " + ID); }));
timer = new Timer { AutoReset = true, Interval = 1000 };
SomeTimerElapsedEventManager.AddListener(timer, new GenericWeakEventListener((t, s, e) => { Console.WriteLine("SomeTimer: " + ID); }));
timer.Start();
}
}
这是发布者的代码:
public class SomeService
{
public event EventHandler<GenericEventArgs<string>> SomeEvent;
public SomeService()
{
System.Timers.Timer timer = new Timer { AutoReset = true, Interval = 1000 };
timer.Elapsed += (sender, args) => { if (SomeEvent != null) SomeEvent(this, new GenericEventArgs<string>(Guid.NewGuid().ToString())); };
timer.Start();
}
}
这是来自 main 方法的代码:
public static void Main(string[] args)
{
SomeService service = new SomeService();
List<SomeClient> clients = new List<SomeClient>();
// Build clients
for (int x = 0; x < 5; x++)
{
clients.Add(new SomeClient(x + 1, service));
}
System.Timers.Timer timer = new Timer { AutoReset = true, Interval = 5000 };
timer.Elapsed += (s, a) =>
{
if (clients.Count == 0)
{
return;
}
Console.WriteLine("Removing\r\n");
clients.RemoveAt(0);
GC.Collect();
};
timer.Start();
Console.ReadLine();
}
这是输出: SomeEvent: 1 SomeEvent: 2 SomeEvent: 3 SomeEvent: 4 SomeEvent: 5 SomeTimer: 2 SomeTimer: 3 SomeTimer: 4 SomeTimer: 1 SomeTimer: 5 SomeEvent: 1 SomeEvent: 2 SomeEvent: 3 SomeEvent: 4 SomeEvent: 5 SomeTimer: 1 SomeTimer: 2 SomeTimer: 3 SomeTimer: 4 SomeTimer: 5 SomeEvent: 1 SomeEvent: 2 SomeEvent: 3 SomeEvent: 4 SomeEvent: 5 SomeTimer: 2 SomeTimer: 3 SomeTimer: 4 SomeTimer: 5 SomeTimer: 1 SomeEvent: 1 SomeEvent: 2 SomeEvent: 3 SomeEvent: 4 SomeEvent: 5 SomeTimer: 1 SomeTimer: 2 SomeTimer: 3 SomeTimer: 5 SomeTimer: 4 SomeEvent: 1 SomeEvent: 2 SomeEvent: 3 SomeEvent: 4 SomeEvent: 5 移除
如果没有通用的弱事件侦听器,那么输出会在没有 1 的情况下继续输出,然后在没有 2 的情况下继续输出,依此类推。