user5147454
user5147454

Reputation:

How to disable subscription to an event from many instances of one type and allow only one?

I have Windows Forms application with one main form (derived from base Form). Other modal forms that could be opened there are derived from my class ManagedForm, which is also derived from Form.
Also I have a static notifier service which fires some events like this:

    public static class NotifierService
    {
        public delegate void NotifierServiceEventHandler(object sender, NotifierServiceEventArgs e);

        private static readonly object Locker = new object();
        private static NotifierServiceEventHandler _notifierServiceEventHandler;

        #region Events

        public static event NotifierServiceEventHandler OnOk
        {
            add
            {
                lock (Locker)
                {
                    _notifierServiceEventHandler += value;

                    if (
                        _notifierServiceEventHandler.GetInvocationList()
                                                    .Count(
                                                        _ =>
                                                        _.Method.DeclaringType != null &&
                                                        value.Method.DeclaringType != null &&
                                                        _.Method.DeclaringType == value.Method.DeclaringType) <= 1)
                        return;

                    _notifierServiceEventHandler -= value;
                }
            }
            remove
            {
                lock (Locker)
                {
                    _notifierServiceEventHandler -= value;
                }
            }
        }

        // and many more events similar to previous...

        #endregion

        #region Event firing methods

        public static void NotifyOk(string fullMessage = "Ok.", string shortMessage = null)
        {
            NotifierServiceEventHandler handler;

            lock (Locker)
            {
                handler = _notifierServiceEventHandler;
            }

            if (handler == null) return;

            handler(typeof (NotifierService),
                    new NotifierServiceEventArgs(StatusType.Ok, fullMessage, shortMessage ?? fullMessage));
        }

        #endregion
    }

So in some places of code these events could be fired like:

NotifierService.NotifyExclamation("Fail!");

In the main form there is StatusStrip control used for notification purposes, and due to main form has subscribtion to these events -- their messages will be shown in the status strip.
BUT!, as I've said earlier, user may open other forms, and these forms could produce others and so on... (they are derived from one class ManagedForm which will be subscribed to NotifierService as soon as it has been created).
In these forms there is another logic how to notify user -- they need to show MessageBoxes with messages. As you can see, I've added some magic in event accessors to allow only one subscriber of any type, because w/o this all opened forms will generate their own MessageBoxes. But when one child ManagedForm has produced another and the second has been closed -- no MessageBoxes will be shown.
What magic should I implement to allow subscription from only first ManagedForm? Many thanks for any ideas.

EDIT: Suggested ideas doesn't solve this issue. I've tried to change event to this:

private static readonly object Locker = new object();

private static EventHandler<NotifierServiceEventArgs> _myEvent;

public static event EventHandler<NotifierServiceEventArgs> OnOk
{
    add
    {
        if (_myEvent == null || _myEvent.GetInvocationList().All(_ => _.Method.DeclaringType != value.Method.DeclaringType))
        {
            _myEvent += value;
        }
    }
    remove
    {
        _myEvent -= value;
    }
}

Then I've open one modal child form and create a situation in which event has been fired by NotifierService. One MessageBox has been generated and shown (that's OK). Afterwards I've opened another modal form from first and create another situation in which another event has been fired. One MessageBox has been generated and shown (that's also OK). Now I'm closing second form and making a situation needed to fire event. No MessageBoxes has been shown (but in the status strip of the main form message of event has been shown correctly, so nothing has been changed from my first implementation).
Should I change something in remove clause? I do not need that only one subscriber should be, I need that each of the subscribers should be of distinct types. Sorry If bad English.

Upvotes: 9

Views: 1406

Answers (8)

kidmosey
kidmosey

Reputation: 400

Subscriptions are useful if you actually want these events to propagate to each form, but that doesn't seem like what you want to do. Given any action, your code is needing to show only one dialog box and update the status text of the main form.

Maybe you should consider using a singleton pattern, instead. By using a static event handler, this is essentially what you are already doing.

public class MainAppForm : Form
{
    static MainAppForm mainAppForm;

    public MainAppForm()
    {
        mainAppForm = this;
    }

    public static void NotifyOk(Form sender, string fullMessage = "Ok.", string shortMessage = null)
    {
        mainAppForm.NotifyOk(sender, fullMessage, shortMessage);
    }

    public void NotifyOk(Form sender, string fullMessage, string shortMessage)
    {
        this.statusStrip.Invoke(delegate {
            this.statusStrip.Text = shortMessage;
        });
    }
}

Upvotes: 0

Ivan Stoev
Ivan Stoev

Reputation: 205849

The way you are trying to solve the problem is fundamentally wrong by design. Your service class defines an event that will be fired under some circumstances. Some clients subscribe to that event, this way requesting to be notified when it happened. This is simply the .NET way of implementing the Observer pattern, so your service (being the subject or observable), should not apply any logic neither at subscribe nor the notify part, thus defeating the whole purpose of the pattern. Hans Passant already pointed to some flaws in your design, but even his solution is not perfect because looking at the event signature, it's totally unclear that only form instance methods are supposed to be registered - one can try using static method, anonymous lambda/method, some class method etc.

So, IMO the following are some of the viable choices you have.

(A) Keep your NotificationService events, but remove any "magic" from both subscribe and notify parts (shortly, use the regular way of defining and firing an event) and put the logic needed in your subscribers:

public static class NotifierService
{
    public delegate void NotifierServiceEventHandler(object sender, NotifierServiceEventArgs e);
    public static event NotifierServiceEventHandler OnOk;
    public static void NotifyOk(string fullMessage = "Ok.", string shortMessage = null)
    {
        var handler = OnOk;
        if (handler != null)
            handler(typeof(NotifierService), new NotifierServiceEventArgs(StatusType.Ok, fullMessage, shortMessage ?? fullMessage));
    }
}

Assuming that only the active form is supposed to handle the notifications, the existing handlers in both your MainForm and ManagedForm would use something like this inside their method body

if (this != ActiveForm) return;
// do the processing

You can even create a base form like this

class NotifiedForm : Form
{
    protected override void OnActivated(EventArgs e)
    {
        base.OnActivated(e);
        NotifierService.OnOk += OnNotifyOK;
        // similar for other events
    }
    protected override void OnDeactivate(EventArgs e)
    {
        base.OnDeactivate(e);
        NotifierService.OnOk -= OnNotifyOK;
        // similar for other events
    }
    protected virtual void OnNotifyOK(object sender, NotifierServiceEventArgs e) { }
    // similar for other events
}

and let your MainForm, ManagedForm (and any other is needed) inherit from that and just override the OnNotifyXXX methods and apply their logic.

To conclude, this approach would keep your service abstract and will leave the decisions to the clients of the service.

(B) If the sole purpose of your service is to act like a notification coordinator specifically for your forms, then you can remove events along with subscribe/unsubscribe parts (since Application.OpenForms and Form.ActiveForm already provide enough information needed) and handle the logic in your service. In order to do that, you'll need some sort of a base interface(s) or forms, and the easiest would be to use a similar approach to what was optional in the option (A) by creating a base form class like this

class NotifiedForm : Form
{
    public virtual void OnNotifyOK(object sender, NotifierServiceEventArgs e) { }
    // similar for other notifications
}

and let your MainForm, ManagedForm and other needed inherit from it. Note that there is no logic here (checking ActiveForm etc.) because now that's the responsibility of the caller. Then the service could be something like this:

public static class NotifierService
{
    public static void NotifyOk(string fullMessage = "Ok.", string shortMessage = null)
    {
        var target = Form.ActiveForm as NotifiedForm;
        if (target != null)
            target.OnNotifyOK(typeof(NotifierService), new NotifierServiceEventArgs(StatusType.Ok, fullMessage, shortMessage ?? fullMessage));
    }
    // similar for other notifications
}

if the logic is to notify only the active form.

Or

public static class NotifierService
{
    public static void NotifyOk(string fullMessage = "Ok.", string shortMessage = null)
    {
        // Could also be a forward for, forach etc.
        for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
        {
            var target = Application.OpenForms[i] as NotifiedForm;
            if (target != null /* && someOtherCritaria(target) */)
            {
                target.OnNotifyOK(typeof(NotifierService), new NotifierServiceEventArgs(StatusType.Ok, fullMessage, shortMessage ?? fullMessage));
                // Could also continue
                break;
            }
        }
    }
    // similar for other notifications
}

if some other logic is needed (which I doubt).

Hope that helps. In any case, option (A) is more flexible and allows much more usage scenarios, but if the usage scenarios are fixed by design, then the option (B) is better because it requires less from the clients (thus being less error prone) and provides a centralized application logic in one place.

Upvotes: 7

Harald Coppoolse
Harald Coppoolse

Reputation: 30512

Are you sure that it is the task of the NotifierService to make sure that only one Form will show the notification?

If you would describe the tasks of a NotifierService, you would describe what it does and "whenever the NotifierService has something to notify, it will notify everyone who said that it wanted to be notified about the notifications"

This would make your notifierservice less dependant of the current application where it is used. If you want a completely different application with for instance only two Forms, where you want both Forms to react on the notifications you could not use this notifierservice.

But in my Forms application only one form may react on the notifications

That is right: it is your Forms application that has this constraint, not the notifierservice. You make a Forms aplication that may use any kind of notifierservice, but whatever notifierservice is used, only one of the Forms in my application may show the notification.

This means that you should have some rule to know whether a form should show the notifications or not

For instance:

  • Only the current form may show the notifications
  • Only the top left form may show the notifications
  • Only the main form may show the notifications, except when the settings form is visible

So let's assume you have something to determine which Form or Forms may react on notifications. This changes upon something happening: a form becomes active, or a form closes, a form becomes invisible, whatever.

Make a Boolean property for a ManagedForm that holds whether it should show notifications:

class ManagedForm
{
    public bool ShowNotifications {get; set;}

    public void OnEventNotification(object sender, ...)
    {
        if (this.ShowNotifications)
        {
            // show the notification
        }
    }

Now someone has to know which form should show the notification. This someone should set property ShowNotification.

For instance if only the active ManagedForm should show the notifications then the ManagedForm can decide for itsels:

public OnFormActiveChanged(object sender, ...)
{
    this.ShowNotifications = this.Form.IsActive;
}

If all red Forms should show the notifications:

public OnFormBackColorChanged(object sender, ...)
{
    this.ShowNotifications = this.Form.BackColor == Color.Red;
}

If you have a lot of Forms, with only a few that show notifications, then a lot events OnShowNotification will be called for nothing, but since this is just a function call it won't be a problem unless you show 1000 forms or so, and I guess in that you have more serious problems.

Summerized

  • Decide the criterium on which a ManagedForm should show the notifications
  • Decide when a different form should show the notifications
  • Create an event handler for when the form changes, let the event handler set property ShowNotification
  • When the event to show the notification occurs, check the property.

Upvotes: 0

Sinatr
Sinatr

Reputation: 22008

To sum up:

  • there are multiple sources of events;
  • there are multiple targets;
  • there are different types of events which have to be processed differently.

Idea to use static manager is ok (unless you have performance issues, then splitting into multiple different message queues is the option), but cheating with subscribing/unsubscribing feels so wrong.

Make a simple event

public enum MessageType { StatusText, MessageBox }

public NotifyEventArgs: EventArgs
{
    public MessageType Type { get; }
    public string Message { get; }

    public NotifyEventArgs(MessageType type, string message)
    {
        Type = type;
        Message = message;
    }
}

public static NotifyManager
{
    public event EventHandler<NotifyMessageArgs> Notify;

    public static OnEventHandler(MessageType type, string message) =>
        Notify?.Invoke(null, new NotifyEventArgs(type, message));
}

Each form has to subscribe to this event when shown and unsubscribe when hidden. Not sure which events are the best here (got used to much to WPF Loaded, Unloaded, but there is no such in winforms, try to use Shown or VisibilityChanged perhaps).

Each form will receive event, but only one has to process MessageBox type (it is safe for all of them to display StatusMessage). For this you need some mechanizm to decide whenever form is the one (used to display message boxes). E.g. it can be active form:

void NotifyManager_Event(object sender, NotifyEventArgs e)
{
    if(e.Type == MessageType.MessageBox && this == Form.ActiveForm)
        MessageBox.Show(this, e.Message);
    else
        statusBar.Text = e.Message;
}

Upvotes: 0

Kapoor
Kapoor

Reputation: 1428

I have made a setup similar to yours & I see the problem.

I'll give 2 working suggestion to fix the issue (you may choose as per the changes required) -

  1. Quickest fix with minimal changes to your original code -

So this is what I understand from the problem situation - You hooked event NotifierService.OnOk to an event handler in class ManagedForm & also wrote code to unhook the event handler from event NotifierService.OnOk when the form closes.

I'm assuming that you wrote the code to unhook the event handler from event NotifierService.OnOk when the form closes But what I'm not sure is that when do you hook event NotifierService.OnOk to its event handler in managed form. Thats critical & I guess thats the only problem in your setup.

I assume you have set it up at a place which happens only once in the lifetime of form - like constructor or Load Event handler. And thats how I could reproduce the problem.

As fix, Just move hooking the event NotifierService.OnOk to its event handler at a place which which is called everytime the form becomes active like something like this -

public partial class ManagedFrom : Form
{

    // this is the fix. Everytime the form comes up. It tries to register itself.
    //The existing magic will consider its request to register only when the other form is closed or if its the 1st of its type.
    protected override void OnActivated(EventArgs e)
    {
        base.OnActivated(e);
        NotifierService.OnOk += NotifierService_OnOk;
    }

No more change needed, your existing logic in the event will take care of rest. I have written the reason as comment in code above.

  1. A little Better way but needs more changes

I would like to relieve the event OnOk form all the additional (& magical) responsibilities, I change the event

 public static event NotifierServiceEventHandler OnOk
    {
        add
        {
            lock (Locker)  // I'm not removing the locks.  May be the publisher works in a multithreaded business layer.
            {
                _notifierServiceEventHandler += value;                  
            }
        }
        remove
        {
            lock (Locker)
            {
                _notifierServiceEventHandler -= value;
            }
        }
    }

Instead the subscriber should know when to Start and when to stop the subscription.

Therefore I change ManagedFrom

 public partial class ManagedFrom : Form
{

    //start the subscription
    protected override void OnActivated(EventArgs e)
    {
        base.OnActivated(e);
        NotifierService.OnOk += NotifierService_OnOk;
    }

    //stop the subscription
    protected override void OnDeactivate(EventArgs e)
    {
        base.OnDeactivate(e);
        NotifierService.OnOk -= NotifierService_OnOk;
    }

In both the suggestions, my intend is to just fix the issue without introducing any new pattern. But do let me know if thats needed. Also do let me know if it was helpful or if you think I took any wrong assumption .

Upvotes: 0

ASh
ASh

Reputation: 35730

i have reduced NotifierService to this:

public static class NotifierService
{
    public static event EventHandler<NotifierServiceEventArgs> OnOk = delegate { };

    public static void NotifyOk(string fullMessage = "Ok.", string shortMessage = null)
    {
        OnOk(typeof(NotifierService), 
             new NotifierServiceEventArgs(StatusType.Ok, fullMessage, shortMessage ?? fullMessage));
    }
}

and then in ManagedForm used this handler

NotifierService.OnOk += Notify;

private void Notify(object sender, NotifierServiceEventArgs e)
{
    // handle event in first open ManagedForm
    if (Application.OpenForms.OfType<ManagedForm>().FirstOrDefault() == this)
    {
       // notification logic
    }
}

if forms are opened as Modal (using ShowDialog()), it is possible to use another variant (according to this question):

private void Notify(object sender, NotifierServiceEventArgs e)
{
    // handle event in active (last shown) ManagedForm
    if (this.CanFocus)
    {
       // notification logic
    }
}

so the idea is that all ManagedForms receive event data and then decide should they do something or not

P.S.: unsubscribe handlers on Dispose

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        NotifierService.OnOk -= Notify;
    }
    // default
    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}

Upvotes: 0

Deepak Bhatia
Deepak Bhatia

Reputation: 1100

I would like you proceed as follows:

  1. Remove the magic from event accessor method and let all the subscribers subscribe to the event. So now you will have your main form and all other forms subscribed to the event.

  2. Now place the magic in your event invocation method. For example in your NotifyOK method, first get the invocation list of deligate, now invoke each deligate one by one using DynamicInvoke or Invoke method of each deligate in the invocation list only if you have not already invoked for the particular DeclaringType. See the algo below:

     public static void NotifyOk(string fullMessage = "Ok.", string shortMessage = null)
    
     {
        NotifierServiceEventHandler handler;
    
        lock (Locker)
        {
            handler = _notifierServiceEventHandler;
        }
    
        if (handler == null) return;
    
        // Get invocation list of handler as you have done in event accessor
    
        //initialise a new List<T> to hold the declaring types
    
        // loop through each member (delegate) of invocation list
    
          // if the current member declaration type is not in List<t>
    
           // Invoke or DynamicInvoke current delegate
           // add the declaration type of current delegate to List<t> 
     }
    

Upvotes: 1

P.K.
P.K.

Reputation: 1772

Try this:?)

private bool _eventHasSubscribers = false;
private EventHandler<MyDelegateType> _myEvent;

public event EventHandler<MyDelegateType> MyEvent
{
   add 
   {
      if (_myEvent == null)
      {
         _myEvent += value;
      }
   }
   remove
   {
      _myEvent -= value;
   }
}

Upvotes: 0

Related Questions