Live Chat Icon For mobile
Live Chat Icon

How do I control the order in which events are fired to multiple subscribers?

Platform: WinForms| Category: Events

In C#, you can do as follows:


		// Use a custom Delegate to hold your subscribers
		private EventHandler myHandlers;
		// Control Click event firing, for example.
		public new event EventHandler Click
		{
			add
			{
				this.myHandlers += value;
			}
			remove
			{
				this.myHandlers -= value;
			}
		}

		protected override void OnClick(EventArgs e)
		{
			// First let my derived classes receive the Click event.
			foreach(Delegate d in this.myHandler.GetInvocationList())
			{
				if(d.Target == this)
				{
					d.DynamicInvoke(new object[]{this, e});
					break;
				}
			}
			// Then let other subscribers receive the Click event.
			foreach(Delegate d in this.myHandler.GetInvocationList())
			{
				if(d.Target != this)
					d.DynamicInvoke(new object[]{this, e});
			}

		}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.