有没有办法指定处理已注册事件代表的顺序或优先级?例如,我有一个事件,我想在任何其他事件之前立即处理,但我希望允许其他对象注册该事件的监听器.如何实现这一目标?
让我们说proc1总是在proc 2之前运行.
class MessageProcessor
{
private DataClient Client;
public MessageProcesser(DataClient dc)
{
Client = dc;
Client.MessageReceived += ProcessMessage;
}
void proc1(MessageEventArgs e)
{
// Process Message
}
}
class DataClient
{
public event MessageReceievedHandler MessageReceived;
}
void main()
{
DataClient dc = new DataClient();
MessageProcessor syncProcessor = new MessageProcessor(dc); // This one is high priority and needs to process all sync immediately when they arrive before any data messages
MessageProcessor dataProcessor= new MessageProcessor(dc); // This one can process the data as it has time when sync messages are not being processed.
// do other stuff
}
Run Code Online (Sandbox Code Playgroud)
这样做的原因是,我有一台服务器通过UDP流发送消息.它将在数据突发之前发送同步消息.我意识到两个处理程序都会在收到同步消息时触发,但为了减少延迟,我希望syncProcessor对象在dataProcessor事件之前处理事件.这将减少正在处理的同步消息的延迟.
此外,我团队中的其他人可能也想要注册事件来处理特定消息.它们可能有自己的对象,它们将注册一个事件(可能不是MessageProcessor),即使Sync消息应该具有尽可能低的延迟.
编辑通过更好的例子使目标更加清晰.
当您多次订阅某个事件时,无法确定触发事件时处理程序的执行顺序.
根据这个答案,订阅订单中的订单,但正如作者所说,这是一个实现细节,你不能依赖它.
您当然可以编写自己的"事件管理器"(参见下面的脏示例),以已知的顺序执行处理程序,但我认为这不是一个好主意.也许您应该重新设计您的活动以取消您的要求.
public class MyClass
{
// Could be anything...
public delegate void MyEventHandler(object sender, EventArgs e);
public event MyEventHandler TestEvent;
public void Test()
{
if (this.TestEvent != null)
{
this.TestEvent(this, EventArgs.Empty);
}
}
}
public class EventManager
{
private List<EventHandler> Handlers = new List<EventHandler>();
public void AddHandler(EventHandler handler)
{
this.Handlers.Add(handler);
}
public void RemoveHandler(EventHandler handler)
{
this.Handlers.Remove(handler);
}
public void Handler(object sender, EventArgs e)
{
foreach (var z in this.Handlers)
{
z.Invoke(sender, e);
}
}
}
private static void Main(string[] args)
{
MyClass test = new MyClass();
EventManager eventManager = new EventManager();
// Subscribes to the event
test.TestEvent += eventManager.Handler;
// Adds two handlers in a known order
eventManager.AddHandler(Handler1);
eventManager.AddHandler(Handler2);
test.Test();
Console.ReadKey();
}
private static void Handler1(object sender, EventArgs e)
{
Console.WriteLine("1");
}
private static void Handler2(object sender, EventArgs e)
{
Console.WriteLine("2");
}
Run Code Online (Sandbox Code Playgroud)
我要冒险说“不”。AFAIK 事件本质上是异步调度的。因此,这将按照绑定的顺序进行发送。但是结果可能会以不同的顺序返回。唯一的方法是在第一个事件的回调中分派第二个事件。这不是特定于语言的,更多的是关于事件的架构。
在您的示例中,由于调用无效,因此不在 proc1 末尾运行 proc2 吗?