How to: Subscribe to Events
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies.
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 execute the Add Event Subscription recipe on an existing C# class.
To add an event subscription
- Using Visual Studio, open an existing smart client solution.
- In Solution Explorer, right-click a C# class, point to Smart Client Factory, and then click Add Event Subscription, as shown in Figure 1.
Note: 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.
|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.