DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
Task.cpp
Go to the documentation of this file.
1#include "pch.h"
10
11namespace DotNetDupe {
12 namespace System {
13 namespace Threading {
14 namespace Tasks {
15
18
20 : m_objAction(objAction), m_eStatus(TaskStatus::Created) {
22 m_pCompletionEvent = SmartPointer<ManualResetEvent>::NewShared(false);
23 }
24
27 }
28
29 void Task::Start() {
31 {
32 Lock<CriticalSection> lock(m_csSync);
33 if (m_eStatus != TaskStatus::Created) {
34 throw InvalidOperationException("Task has already been started or executed.");
35 }
36 m_eStatus = TaskStatus::WaitingToRun;
37 }
38
40 ThreadPool::QueueUserWorkItem(&Task::ThreadPoolCallback, this);
41 }
42
43 void Task::Wait() {
45 m_pCompletionEvent->WaitOne();
46 }
47
48 bool Task::Wait(int iMillisecondsTimeout) {
50 try {
51 return m_pCompletionEvent->WaitOne(iMillisecondsTimeout);
52 } catch (const TimeoutException&) {
53 return false;
54 }
55 }
56
59 Lock<CriticalSection> lock(const_cast<CriticalSection&>(m_csSync));
60 return m_eStatus;
61 }
62
63 bool Task::GetIsCompleted() const {
65 Lock<CriticalSection> lock(const_cast<CriticalSection&>(m_csSync));
66 return m_eStatus == TaskStatus::RanToCompletion ||
67 m_eStatus == TaskStatus::Faulted ||
68 m_eStatus == TaskStatus::Canceled;
69 }
70
71 bool Task::GetIsFaulted() const {
73 Lock<CriticalSection> lock(const_cast<CriticalSection&>(m_csSync));
74 return m_eStatus == TaskStatus::Faulted;
75 }
76
77 bool Task::GetIsCanceled() const {
79 Lock<CriticalSection> lock(const_cast<CriticalSection&>(m_csSync));
80 return m_eStatus == TaskStatus::Canceled;
81 }
82
86 RetainTask(pTask);
87 pTask->Start();
88 return pTask;
89 }
90
91 void Task::Execute() {
93 {
94 Lock<CriticalSection> lock(m_csSync);
95 m_eStatus = TaskStatus::Running;
96 }
97 try {
98 if (m_objAction) m_objAction();
99 Lock<CriticalSection> lock(m_csSync);
100 m_eStatus = TaskStatus::RanToCompletion;
101 } catch (...) {
102 Lock<CriticalSection> lock(m_csSync);
103 m_eStatus = TaskStatus::Faulted;
104 }
106 m_pCompletionEvent->Set();
107 ReleaseTask(this);
108 }
109
110 void Task::ThreadPoolCallback(Object* pState) {
112 Task* pTask = static_cast<Task*>(pState);
113 if (pTask == nullptr) return;
114
116 SmartPointer<Task> spSelf(nullptr);
117 {
118 Lock<CriticalSection> lock(s_csActiveTasks);
119 for (int i = 0; i < s_pvActiveTasks.GetCount(); ++i) {
120 if (s_pvActiveTasks[i].Get() == pTask) {
121 spSelf = s_pvActiveTasks[i];
122 break;
123 }
124 }
125 }
126
128 if (!spSelf.IsNull()) {
129 spSelf->Execute();
130 }
131 }
132
133 void Task::RetainTask(SmartPointer<Task> pTask) {
135 Lock<CriticalSection> lock(s_csActiveTasks);
136 s_pvActiveTasks.Add(pTask);
137 }
138
139 void Task::ReleaseTask(Task* pTask) {
141 Lock<CriticalSection> lock(s_csActiveTasks);
142 for (int i = 0; i < s_pvActiveTasks.GetCount(); ++i) {
143 if (s_pvActiveTasks[i].Get() == pTask) {
144 s_pvActiveTasks.RemoveAt(i);
145 break;
146 }
147 }
148 }
149
150 }
151 }
152 }
153}
Defines the exception thrown when a method call is invalid for the object's current state.
Represents a strongly typed list of objects that can be accessed by index mirroring ....
Provides an RAII-style scoped lock wrapper around synchronization primitives.
Serves as the base class for system exceptions across the library.
Represents an asynchronous operation mirroring .NET System.Threading.Tasks.Task.
Provides a pool of threads that can be used to execute tasks and work items.
Defines the exception thrown when the time allotted for a process or operation has expired.
Represents an unknown or unmapped exception encountered during execution.
Represents a strongly typed list of objects accessible by index.
Definition List.h:29
InvalidOperationException(const String &sMessage)
Initializes a new instance of the InvalidOperationException class with a specified error message.
A unified smart pointer that supports both unique and shared ownership semantics.
static SmartPointer< T > NewShared()
Creates a Shared SmartPointer, default constructing T.
Provides a re-entrant mutual exclusion primitive for thread synchronization.
Provides an RAII-style scoped lock wrapper around synchronization primitives.
Definition Lock.h:21
Represents an asynchronous operation.
Definition Task.h:27
virtual ~Task()
Destructor for Task.
Definition Task.cpp:25
void Wait()
Waits indefinitely for the Task to complete execution.
Definition Task.cpp:43
bool GetIsCompleted() const
Gets whether this Task has completed.
Definition Task.cpp:63
Task(Action<> objAction)
Initializes a new Task with the specified action delegate.
Definition Task.cpp:19
bool GetIsCanceled() const
Gets whether this Task instance has completed execution due to being canceled.
Definition Task.cpp:77
static SmartPointer< Task > Run(Action<> objAction)
Queues the specified work to run on the ThreadPool and returns a Task representing that work.
Definition Task.cpp:83
void Start()
Starts the Task, scheduling it for execution to the ThreadPool.
Definition Task.cpp:29
TaskStatus GetStatus() const
Gets the TaskStatus of this task.
Definition Task.cpp:57
bool GetIsFaulted() const
Gets whether the Task completed due to an unhandled exception.
Definition Task.cpp:71
static bool QueueUserWorkItem(WaitCallback callback)
Queues a method for execution. The method executes when a thread pool thread becomes available.
The exception that is thrown when the time allotted for a process or operation has expired.
TaskStatus
Represents the current stage in the lifecycle of a Task.
Definition TaskStatus.h:12
static CriticalSection s_csActiveTasks
Definition Task.cpp:17
static Collections::Generic::List< SmartPointer< Task > > s_pvActiveTasks
Definition Task.cpp:16