CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
task_utils.h
1
2#pragma once
3
4#include <functional>
5#include <system/object.h>
6#include <system/collections/ienumerable.h>
7#include <system/threading/tasks/task.h>
8#include <system/threading/tasks/result_task.h>
9#include <system/threading/tasks/async.h>
10#include <system/array.h>
11
12namespace System { namespace Threading {
13
14class CancellationToken;
15
16namespace Tasks {
17
22ASPOSECPP_SHARED_API TaskPtr Delay(int32_t millisecondsDelay);
23ASPOSECPP_SHARED_API TaskPtr Delay(int32_t millisecondsDelay, const CancellationToken& cancellationToken);
24
25//
26// Summary:
27// Creates a System.Threading.Tasks.Task that has completed with a specified exception.
28//
29// Parameters:
30// exception:
31// The exception with which to complete the task.
32//
33// Returns:
34// The faulted task.
35ASPOSECPP_SHARED_API TaskPtr FromException(const Exception& exception);
36
37//
38// Summary:
39// Creates a System.Threading.Tasks.Task`1 that's completed with a specified exception.
40//
41// Parameters:
42// exception:
43// The exception with which to complete the task.
44//
45// Type parameters:
46// TResult:
47// The type of the result returned by the task.
48//
49// Returns:
50// The faulted task.
51template<typename TResult>
53{
54 return MakeAsync<TResult>([=](System::Details::ResultAsyncContext<TResult>& __context) { exception.Throw(); });
55}
56
57//
58// Summary:
59// Creates a System.Threading.Tasks.Task`1 that's completed successfully with the
60// specified result.
61//
62// Parameters:
63// result:
64// The result to store into the completed task.
65//
66// Type parameters:
67// TResult:
68// The type of the result returned by the task.
69//
70// Returns:
71// The successfully completed task.
72template <typename TResult>
74{
75 return MakeAsync<TResult>([=](System::Details::ResultAsyncContext<TResult>& __context) { __context.Return(result); });
76}
77
78ASPOSECPP_SHARED_API TaskPtr Run(const Action<>& action);
79ASPOSECPP_SHARED_API TaskPtr Run(const Action<>& action, const CancellationToken& cancellationToken);
80
81template <typename TResult>
83{
84 auto result = MakeObject<ResultTask<TResult>>(function);
85 result->Start();
86 return result;
87}
88
89ASPOSECPP_SHARED_API TaskPtr WhenAll(const ArrayPtr<TaskPtr>& tasks);
91
92//
93// Summary:
94// Creates a task that will complete when all of the System.Threading.Tasks.Task`1
95// objects in an enumerable collection have completed.
96//
97// Parameters:
98// tasks:
99// The tasks to wait on for completion.
100//
101// Type parameters:
102// TResult:
103// The type of the completed task.
104//
105// Returns:
106// A task that represents the completion of all of the supplied tasks.
107//
108// Exceptions:
109// T:System.ArgumentNullException:
110// The tasks argument was null.
111//
112// T:System.ArgumentException:
113// The tasks collection contained a null task.
114template <typename T>
116{
117 return WhenAll(tasks->LINQ_ToArray());
118}
119
120//
121// Summary:
122// Creates a task that will complete when all of the System.Threading.Tasks.Task`1
123// objects in an array have completed.
124//
125// Parameters:
126// tasks:
127// The tasks to wait on for completion.
128//
129// Type parameters:
130// TResult:
131// The type of the completed task.
132//
133// Returns:
134// A task that represents the completion of all of the supplied tasks.
135//
136// Exceptions:
137// T:System.ArgumentNullException:
138// The tasks argument was null.
139//
140// T:System.ArgumentException:
141// The tasks array contained a null task.
142template <typename T>
144{
145 //return WhenAll(tasks->LINQ_Cast<TaskPtr>())->ContinueWith<T>
146
147 return MakeAsync<ArrayPtr<T>>([=](System::Details::ResultAsyncContext<ArrayPtr<T>>& __context)
148 {
149 for (auto& task : tasks)
150 {
151 if (!task->get_IsCompleted())
152 {
153 return;
154 }
155 }
156
157 auto size = tasks->get_Length();
158 auto result = MakeArray<T>(size);
159 for (auto i = 0; i < size; ++ i)
160 {
161 result[i] = tasks[i]->get_Result();
162 }
163 __context.Return(result);
164 });
165}
166
167}}} // namespace System::Threading::Tasks
Interface of object providing enumerator on contained elements.
Definition: ienumerable.h:25
Template that represents wrapper of exceptions that are derived from Exception class.
Definition: exception.h:113
Function delegate. This type should be allocated on stack and passed to functions by value or by refe...
Definition: func.h:82
Propagates notification that operations should be canceled. This class provides a mechanism for coope...
Definition: cancellation_token.h:43
void Start()
Starts the task execution using the default scheduler.
TaskPtr Delay(int32_t millisecondsDelay)
Creates a task that completes after a time delay.
TaskPtr WhenAll(const ArrayPtr< TaskPtr > &tasks)
TaskPtr Run(const Action<> &action)
TaskPtr FromException(const Exception &exception)
RTaskPtr< TResult > FromResult(TResult result)
Definition: task_utils.h:73
Definition: db_command.h:9
MulticastDelegate< void(Args...)> Action
Delegate type that references methods that have no return value.
Definition: action.h:40