Event Publication/Subscription and clean up

Topics: CAB & Smart Client Software Factory
Jul 20, 2006 at 5:50 AM
originally posted by: ForumUserRCC

I have both a CommandHandler and an EventPublication.
In the CommandHandler, i check to see if the EventHandler is null and if not, "raise" the event.
If I do nothing else, when I run the code the EventHandler is never null.

public event EventHandler<DataEventArg<string>> MyEvent;

public void OnClose(object sender, EventArg e)
if (MyEvent != null)
MyEvent(this, new DataEventArg<string>("Hello");

Since there are no subscriptions, I would anticipate MyEvent to be null. It isn't.

Then as a follow up - using SCSF and CAB, how does a module UNsubscribe to an event?
Scenario is i have the Shell, and it detects a Menu item is clicked. Before it can do the action for that item, if there are any modules/views open in the workspaces, they need to clean up and close.
I was thinking that I could detect when the EventHandler is null, then there are no other subscribers and Icould do the action. But first - it never seems to be null; and two, how do I end subscriptions to the event? Does closing/removing the view from the WorkItem also remove any subscriptions?

Can somebody point me to either documentation on this or give me a few ideas on what is happening?
Jul 20, 2006 at 12:48 PM
originally posted by: askew


I have tested publishing an 'OnCloseQuery' from the Shell and subscriptions in the modules.
You can pass an "Editable" EventArgs<T> or similar class around the modules and each will have the chance to set the <T> to false, etc, to prevent closing. The Shell could continue to ask until the modules are ready... HTH
Jul 21, 2006 at 6:29 AM
originally posted by: ForumUserRCC

I took the suggestion and did create an "EditableEventArgs" and I use that one for this particular event. The suggested code works the way I needed.