1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4 
5 namespace System.Activities.Tracking
6 {
7     using System;
8     using System.Runtime;
9 
10     public abstract class TrackingParticipant
11     {
TrackingParticipant()12         protected TrackingParticipant()
13         {
14         }
15 
16         public virtual TrackingProfile TrackingProfile
17         {
18             get;
19             set;
20         }
21 
22         [Fx.Tag.InheritThrows(From = "Track", FromDeclaringType = typeof(TrackingParticipant))]
BeginTrack(TrackingRecord record, TimeSpan timeout, AsyncCallback callback, object state)23         protected internal virtual IAsyncResult BeginTrack(TrackingRecord record, TimeSpan timeout, AsyncCallback callback, object state)
24         {
25             return new TrackAsyncResult(this, record, timeout, callback, state);
26         }
27 
28         [Fx.Tag.InheritThrows(From = "Track", FromDeclaringType = typeof(TrackingParticipant))]
EndTrack(IAsyncResult result)29         protected internal virtual void EndTrack(IAsyncResult result)
30         {
31             TrackAsyncResult.End(result);
32         }
33 
34         [Fx.Tag.Throws(typeof(Exception), "extensibility point")]
35         [Fx.Tag.Throws.Timeout("Tracking data could not be saved before the timeout")]
Track(TrackingRecord record, TimeSpan timeout)36         protected internal abstract void Track(TrackingRecord record, TimeSpan timeout);
37 
38         class TrackAsyncResult : AsyncResult
39         {
40             static Action<object> asyncExecuteTrack = new Action<object>(ExecuteTrack);
41             TrackingParticipant participant;
42             TrackingRecord record;
43             TimeSpan timeout;
44 
TrackAsyncResult(TrackingParticipant participant, TrackingRecord record, TimeSpan timeout, AsyncCallback callback, object state)45             public TrackAsyncResult(TrackingParticipant participant, TrackingRecord record, TimeSpan timeout, AsyncCallback callback, object state)
46                 : base(callback, state)
47             {
48                 this.participant = participant;
49                 this.record = record;
50                 this.timeout = timeout;
51                 ActionItem.Schedule(asyncExecuteTrack, this);
52             }
53 
End(IAsyncResult result)54             public static void End(IAsyncResult result)
55             {
56                 AsyncResult.End<TrackAsyncResult>(result);
57             }
58 
ExecuteTrack(object state)59             static void ExecuteTrack(object state)
60             {
61                 TrackAsyncResult thisPtr = (TrackAsyncResult)state;
62                 thisPtr.TrackCore();
63             }
64 
TrackCore()65             void TrackCore()
66             {
67                 Exception participantException = null;
68                 try
69                 {
70                     this.participant.Track(this.record, this.timeout);
71                 }
72                 catch (Exception exception)
73                 {
74                     if (Fx.IsFatal(exception))
75                     {
76                         throw;
77                     }
78 
79                     participantException = exception;
80                 }
81                 base.Complete(false, participantException);
82             }
83         }
84 
85     }
86 }
87