Listening to versioned events


Summary
The IVersionEvents and the IVersionEvents2 interfaces expose several events that event handlers can listen to, allowing custom applications and extensions to respond to versioning events, such as reconciliation and posting. This topic explains how to create event listeners and handlers for these events in C# and VB .NET.


About listening to versioned events

Listeners can subscribe to events from the event interfaces of a version, IVersionEvents_Event and IVersionEvents2_Event. These interfaces can be accessed by casting an IVersion reference.
IVersionEvents_Event publishes the following events:
  • OnConflictsDetected
  • OnReconcile
  • OnRedefineVersion
  • OnRefreshVersion
IVersionEvents2_Event publishes the following additional events:
  • OnArchiveUpdated
  • OnBeginReconcile
  • OnDeleteVersion
  • OnPost
IVersionEvents2_Event does not extend IVersionEvents_Event.
  • A common practice for implementing versioned event handlers is to create a class dedicated to listening to events, an event listener.

Create an event handler

An event handler is a method within the event listener that defines the application's behavior in response to an event. Event handlers must match the return type and parameter types of the published events, but can be given any name. The following code example shows an event handler for the OnReconcile event without any behavior:
[C#]
public void OnReconcile(String targetVersionName, Boolean hasConflicts)
{
    // TODO: Implement the event handling.
}
[VB.NET]
Public Sub OnReconcile(ByVal targetVersionName As String, ByVal hasConflicts As Boolean)
    ' TODO: Implement the event handling.
End Sub

Add handlers to events

Adding a handler to an event varies slightly between C# and VB .NET. See the following:
  • C#—Create an instance of the event's delegate type to encapsulate the event handler. The delegate is instantiated much like a class, by using the new keyword. 
  • VB .NET—AddressOf operator is used to create a procedure delegate for the method.
The delegate instance can then be added to the appropriate event on the event interface. In C#, the += operator is used and in VB .NET, the AddHandler statement is used.
The following code example is an event listener's constructor, which instantiates a delegate and adds it to the IVersionEvents.OnReconcile event. To tightly couple an event listener with a version, the event listener's constructor can accept an IVersion parameter, then cast it to the event interface.
[C#]
public EventListener(IVersion version)
{
    // Cast the version to the event interface.
    IVersionEvents_Event versionEvent = (IVersionEvents_Event)version; 

    // Instantiate the delegate type and add it to the event.
    versionEvent.OnReconcile += new IVersionEvents_OnReconcileEventHandler(OnReconcile);
}
[VB.NET]
Public Sub New(ByVal version As IVersion)
    ' Cast the version to the event interface.
    Dim versionEvent As IVersionEvents_Event = CType(version, IVersionEvents_Event)

    ' Create a procedure delegate and add it to the event.
    AddHandler versionEvent.OnReconcile, AddressOf OnReconcile
End Sub

Instantiate a listener

Once the listener is completed, it can be used within custom applications. Instantiating a listener is shown in the following code example:
[C#]
// Get the version to be listened to.
IVersion version = versionedWorkspace.FindVersion("QA");

// Create a listener.
EventListener eventListener = new EventListener(version);
[VB.NET]
' Get the version to be listened to.
Dim version As IVersion = versionedWorkspace.FindVersion("QA")

' Create a listener.
Dim eventListener As EventListener = New EventListener(version)

Complete listener implementation

The following code example shows the full implementation of the previously described event listener:
[C#]
public class EventListener
{
    public EventListener(IVersion version)
    {
        // Cast the version to the event interface.
        IVersionEvents_Event versionEvent = (IVersionEvents_Event)version; 

        // Instantiate the delegate type and add it to the event.
        versionEvent.OnReconcile += new IVersionEvents_OnReconcileEventHandler(OnReconcile);
    }

    public void OnReconcile(String targetVersionName, Boolean hasConflicts)
    {
        // TODO: Implement the event handling.
    }
}
[VB.NET]
Public Class EventListener
    Public Sub New(ByVal version As IVersion)
        ' Cast the version to the event interface.
        Dim versionEvent As IVersionEvents_Event = CType(version, IVersionEvents_Event)

        ' Create a procedure delegate and add it to the event.
        AddHandler versionEvent.OnReconcile, AddressOf OnReconcile
    End Sub

    Public Sub OnReconcile(ByVal targetVersionName As String, ByVal hasConflicts As Boolean)
        ' TODO: Implement the event handling.
    End Sub
End Class


See Also:

How to wire ArcObjects .NET events




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine