Event Publication/Subscription and clean up

Topics: CAB & Smart Client Software Factory
Jul 20, 2006 at 4: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.

EventPublication("EventName",PublicationScope.Global)
public event EventHandler<DataEventArg<string>> MyEvent;

CommandHandler("Close",ThreadOption.UI)
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?
Thanks
Jul 20, 2006 at 11:48 AM
originally posted by: askew

http://www.gotdotnet.com/codegallery/messageboard/thread.aspx?id=941d2228-3bb5-42fd-8004-c08595821170&mbid=62788f36-a963-4feb-8470-20f1a844ed2a&threadid=9630b8a3-9bba-4131-83b3-728812f34d35

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 5:29 AM
originally posted by: ForumUserRCC

Thanks!
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.