请注意,我不认为这是完整的副本。有类似 问题,但没有一个以这种方式解决问题。
我有多个线程在等待“取消”事件。但是Cancel事件包含一个key(algoIdent),它允许我将回调定向到特定线程。
我想使用一个字典,其字典具有algoIdent的键和CallBack委托的值。
但是,我不知道如何创建事件的新实例(我不是要添加侦听器)。注释掉的代码显示了如何为单个侦听器执行此操作,但是我不知道在字典的Value部分中要放置什么。Add():
public delegate void delProcessCancel(string algoIdent, string callBackMessage);
//public event delProcessCancel ProcessCancelListeners;
public Dictionary<string, delProcessCancel > ProcessCancelListenerDict = new Dictionary<string, delProcessCancel>();
public void AddProcessCancelListener(string algoIdent, delProcessCancel _delProcessCancel)
{
if (!ProcessCancelListenerDict.ContainsKey(algoIdent))
{
ProcessCancelListenerDict.Add(algoIdent, event _delProcessCancel );
}
else
{
}
}
答案 0 :(得分:0)
我回答了我自己的问题,因为我认为您可以在新班级创建一个新活动。因此,我创建了一个wrpper。
有人觉得这样做有问题吗?
public class ProcessCancelEventWrapper
{
public event delProcessCancel ProcessCancelListeners;
public ProcessCancelEventWrapper(delProcessCancel _delProcessCancel)
{
ProcessCancelListeners += _delProcessCancel;
}
public void Raise(string algoIdent, string callBackMessage)
{
ProcessCancelListeners(algoIdent, callBackMessage);
}
}
public delegate void delProcessCancel(string algoIdent, string callBackMessage);
public Dictionary<string, ProcessCancelEventWrapper> ProcessCancelListenerDict = new Dictionary<string, ProcessCancelEventWrapper>();
public enum ListenerOption { Add, Delete, };
public void ManageProcessCancelListener(string algoIdent, delProcessCancel _delProcessCancel, ListenerOption listenerOption)
{
if (listenerOption==ListenerOption.Add)
{
if (!ProcessCancelListenerDict.ContainsKey(algoIdent))
{
ProcessCancelListenerDict.Add(algoIdent, new ProcessCancelEventWrapper(_delProcessCancel));
}
else
{
ProcessCancelListenerDict[algoIdent].ProcessCancelListeners += _delProcessCancel;
}
}
else if (listenerOption == ListenerOption.Delete)
{
if (!ProcessCancelListenerDict.ContainsKey(algoIdent))
{
}
else
{
ProcessCancelListenerDict[algoIdent].ProcessCancelListeners -= _delProcessCancel;
}
}
}
public void RaiseProcessCancelListener(string algoIdent, string callBackMessage)
{
if (!ProcessCancelListenerDict.ContainsKey(algoIdent))
{
}
else
{
ProcessCancelListenerDict[algoIdent].Raise(algoIdent, callBackMessage);
}
}