aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs
blob: 2a9441ad6ab02def94537f60dbe92911b5ed50ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.PMR.Printing;

namespace Tango.Integration.Operation
{
    /// <summary>
    /// Represents a <see cref="MachineOperator.Print(Observables.Job, Observables.ProcessParametersTable)"/> handler.
    /// </summary>
    public class JobHandler
    {
        private Action _cancelAction;

        #region Events

        /// <summary>
        /// Occurs when a job status has been received.
        /// </summary>
        public event EventHandler<JobStatus> StatusReceived;

        /// <summary>
        /// Occurs when the job has failed.
        /// </summary>
        public event EventHandler<Exception> Failed;

        /// <summary>
        /// Occurs when the job has completed successfully.
        /// </summary>
        public event EventHandler Completed;

        /// <summary>
        /// Occurs when the job has been canceled.
        /// </summary>
        public event EventHandler Canceled;

        #endregion

        #region Properties

        /// <summary>
        /// Gets a value indicating whether this handler job has been canceled.
        /// </summary>
        public bool IsCanceled { get; internal set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="JobHandler"/> class.
        /// </summary>
        public JobHandler()
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="JobHandler"/> class.
        /// </summary>
        /// <param name="cancelAction">The cancel action.</param>
        internal JobHandler(Action cancelAction) : this()
        {
            _cancelAction = () => { IsCanceled = true; cancelAction(); };
        }

        #endregion

        #region Internal Methods

        /// <summary>
        /// Raises the status received event.
        /// </summary>
        /// <param name="status">The status.</param>
        internal void RaiseStatusReceived(JobStatus status)
        {
            StatusReceived?.Invoke(this, status);
        }

        /// <summary>
        /// Raises the failed event.
        /// </summary>
        /// <param name="ex">The ex.</param>
        internal void RaiseFailed(Exception ex)
        {
            Failed?.Invoke(this, ex);
        }

        /// <summary>
        /// Raises the completed event.
        /// </summary>
        internal void RaiseCompleted()
        {
            Completed?.Invoke(this, new EventArgs());
        }

        /// <summary>
        /// Raises the canceled event.
        /// </summary>
        internal void RaiseCanceled()
        {
            Canceled?.Invoke(this, new EventArgs());
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Cancels the associated job.
        /// </summary>
        public void Cancel()
        {
            _cancelAction();
        }

        #endregion
    }
}