Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit a2418fb

Browse files
committed
Update code with Task.FromCanceled/FromException/CompletedTask
The codebase was using TaskCompletionSource to create already canceled/faulted tasks. With Task.FromCanceled/FromException, this is no longer necessary. It was also using TaskCompletionSource to create an already completed task; with FromResult that wasn't necessary previously anyway, but we can improve upon it regardless with CompletedTask.
1 parent ef6c1e1 commit a2418fb

File tree

4 files changed

+17
-36
lines changed

4 files changed

+17
-36
lines changed

src/System.Data.Common/src/System/Data/Common/AdapterUtil.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,6 @@ internal static class ADP
1616
// location so that the catcher of the exception will have the appropriate call stack.
1717
// This class is used so that there will be compile time checking of error messages.
1818

19-
static internal Task<T> CreatedTaskWithException<T>(Exception ex)
20-
{
21-
TaskCompletionSource<T> completion = new TaskCompletionSource<T>();
22-
completion.SetException(ex);
23-
return completion.Task;
24-
}
25-
26-
static internal Task<T> CreatedTaskWithCancellation<T>()
27-
{
28-
TaskCompletionSource<T> completion = new TaskCompletionSource<T>();
29-
completion.SetCanceled();
30-
return completion.Task;
31-
}
32-
33-
3419
// NOTE: Initializing a Task in SQL CLR requires the "UNSAFE" permission set (http://msdn.microsoft.com/en-us/library/ms172338.aspx)
3520
// Therefore we are lazily initializing these Tasks to avoid forcing customers to use the "UNSAFE" set when they are actually using no Async features
3621
static private Task<bool> s_trueTask = null;

src/System.Data.Common/src/System/Data/Common/DbCommand.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public virtual Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToke
150150
{
151151
if (cancellationToken.IsCancellationRequested)
152152
{
153-
return ADP.CreatedTaskWithCancellation<int>();
153+
return Task.FromCanceled<int>(cancellationToken);
154154
}
155155
else
156156
{
@@ -167,7 +167,7 @@ public virtual Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToke
167167
catch (Exception e)
168168
{
169169
registration.Dispose();
170-
return ADP.CreatedTaskWithException<int>(e);
170+
return Task.FromException<int>(e);
171171
}
172172
}
173173
}
@@ -196,7 +196,7 @@ protected virtual Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior be
196196
{
197197
if (cancellationToken.IsCancellationRequested)
198198
{
199-
return ADP.CreatedTaskWithCancellation<DbDataReader>();
199+
return Task.FromCanceled<DbDataReader>(cancellationToken);
200200
}
201201
else
202202
{
@@ -213,7 +213,7 @@ protected virtual Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior be
213213
catch (Exception e)
214214
{
215215
registration.Dispose();
216-
return ADP.CreatedTaskWithException<DbDataReader>(e);
216+
return Task.FromException<DbDataReader>(e);
217217
}
218218
}
219219
}
@@ -227,7 +227,7 @@ public virtual Task<object> ExecuteScalarAsync(CancellationToken cancellationTok
227227
{
228228
if (cancellationToken.IsCancellationRequested)
229229
{
230-
return ADP.CreatedTaskWithCancellation<object>();
230+
return Task.FromCanceled<object>(cancellationToken);
231231
}
232232
else
233233
{
@@ -244,7 +244,7 @@ public virtual Task<object> ExecuteScalarAsync(CancellationToken cancellationTok
244244
catch (Exception e)
245245
{
246246
registration.Dispose();
247-
return ADP.CreatedTaskWithException<object>(e);
247+
return Task.FromException<object>(e);
248248
}
249249
}
250250
}

src/System.Data.Common/src/System/Data/Common/DbConnection.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,22 @@ public Task OpenAsync()
115115

116116
public virtual Task OpenAsync(CancellationToken cancellationToken)
117117
{
118-
TaskCompletionSource<object> taskCompletionSource = new TaskCompletionSource<object>();
119-
120118
if (cancellationToken.IsCancellationRequested)
121119
{
122-
taskCompletionSource.SetCanceled();
120+
return Task.FromCanceled(cancellationToken);
123121
}
124122
else
125123
{
126124
try
127125
{
128126
Open();
129-
taskCompletionSource.SetResult(null);
127+
return Task.CompletedTask;
130128
}
131129
catch (Exception e)
132130
{
133-
taskCompletionSource.SetException(e);
131+
return Task.FromException(e);
134132
}
135133
}
136-
137-
return taskCompletionSource.Task;
138134
}
139135

140136
public void Dispose()

src/System.Data.Common/src/System/Data/Common/DbDataReader.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ virtual public Task<T> GetFieldValueAsync<T>(int ordinal, CancellationToken canc
188188
{
189189
if (cancellationToken.IsCancellationRequested)
190190
{
191-
return ADP.CreatedTaskWithCancellation<T>();
191+
return Task.FromCanceled<T>(cancellationToken);
192192
}
193193
else
194194
{
@@ -198,7 +198,7 @@ virtual public Task<T> GetFieldValueAsync<T>(int ordinal, CancellationToken canc
198198
}
199199
catch (Exception e)
200200
{
201-
return ADP.CreatedTaskWithException<T>(e);
201+
return Task.FromException<T>(e);
202202
}
203203
}
204204
}
@@ -216,7 +216,7 @@ virtual public Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellat
216216
{
217217
if (cancellationToken.IsCancellationRequested)
218218
{
219-
return ADP.CreatedTaskWithCancellation<bool>();
219+
return Task.FromCanceled<bool>(cancellationToken);
220220
}
221221
else
222222
{
@@ -226,7 +226,7 @@ virtual public Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellat
226226
}
227227
catch (Exception e)
228228
{
229-
return ADP.CreatedTaskWithException<bool>(e);
229+
return Task.FromException<bool>(e);
230230
}
231231
}
232232
}
@@ -244,7 +244,7 @@ virtual public Task<bool> ReadAsync(CancellationToken cancellationToken)
244244
{
245245
if (cancellationToken.IsCancellationRequested)
246246
{
247-
return ADP.CreatedTaskWithCancellation<bool>();
247+
return Task.FromCanceled<bool>(cancellationToken);
248248
}
249249
else
250250
{
@@ -254,7 +254,7 @@ virtual public Task<bool> ReadAsync(CancellationToken cancellationToken)
254254
}
255255
catch (Exception e)
256256
{
257-
return ADP.CreatedTaskWithException<bool>(e);
257+
return Task.FromException<bool>(e);
258258
}
259259
}
260260
}
@@ -268,7 +268,7 @@ virtual public Task<bool> NextResultAsync(CancellationToken cancellationToken)
268268
{
269269
if (cancellationToken.IsCancellationRequested)
270270
{
271-
return ADP.CreatedTaskWithCancellation<bool>();
271+
return Task.FromCanceled<bool>(cancellationToken);
272272
}
273273
else
274274
{
@@ -278,7 +278,7 @@ virtual public Task<bool> NextResultAsync(CancellationToken cancellationToken)
278278
}
279279
catch (Exception e)
280280
{
281-
return ADP.CreatedTaskWithException<bool>(e);
281+
return Task.FromException<bool>(e);
282282
}
283283
}
284284
}

0 commit comments

Comments
 (0)