How to: Subscribe to Events

How to: Subscribe to Events

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies.
This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
To create client business applications using current Microsoft technologies, see patterns & practices' Prism.

You use the Add Event Subscription recipe to subscribe to an event from a Composite UI Application Block module or the Shell project. This recipe adds code to an existing C# class to subscribe an existing event topic.

You must have an existing smart client solution. The event topic constant must be defined in the EventTopicNames class. This means you must first create your event topic with the Add Event Publication recipe before you add your event subscription.

You execute the Add Event Subscription recipe on an existing C# class.

To add an event subscription

  1. Using Visual Studio, open an existing smart client solution.
  2. In Solution Explorer, right-click a C# class, point to Smart Client Factory, and then click Add Event Subscription, as shown in Figure 1.


    Figure 1

    Add Event Subscription recipe menu item
    Note: You cannot add a subscription for an event topic to the same class that contains the publication of that event topic.

The recipe displays a wizard page that prompts you to enter the following information:

  • The event topic name. You can use the drop-down list box to select an existing event topic name.
  • The threading option. You can choose Publisher, Background, or UserInterface. The default is UserInterface. Use the following descriptions to determine the threading option to use:
    • UserInterface. Use this option if the code in the event handler will interact with controls in the user interface.
    • Background. Use this option to run the subscription in a background thread.
    • Publisher. Use this option to run the subscription in the same thread as the publisher.
  • The event argument type. The default is System.EventArgs. The event broker system uses .NET Framework events and delegates in the underlying code. You can use the arguments within the event to pass information between the publisher and subscriber.
  • Ff699181.e14cf4d5-1e52-49bd-a42d-f6b715b2d036(en-us,PandP.10).png

    Figure 2

    Add Event Subscription wizard
Note: Because the event broker system uses .NET Framework events and delegates in the underlying code, it is possible to change the EventArgs in a subscriber and for other subscribers that are called later to access the modified EventArgs instead of the one sent by the publisher. It is also possible to use the EventArgs to pass data back to a publisher from a subscriber. However, this behavior should be avoided because you cannot guarantee the order in which event subscribers are called.

The recipe will modify the C# class that is selected when you launch the recipe. The recipe will modify this class and add a method with an EventSubscription attribute. This method will execute when the event is raised. The following code is an example of the code that the recipe adds.

[EventSubscription(EventTopicNames.StatusUpdate, ThreadOption.UserInterface)]
public void OnStatusUpdate(object sender, EventArgs eventArgs)
  // TODO: Add your code here

After running the Add Event Subscription recipe, write the business logic that responds to the event. You need to write add code in the method generated by the recipe to handle the event that you subscribed to.

© 2016 Microsoft