Skip to content

Commit c314f81

Browse files
committed
Add TaskFactoryExtensions
1 parent fd3be55 commit c314f81

File tree

5 files changed

+164
-4
lines changed

5 files changed

+164
-4
lines changed

src/.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,10 @@ dotnet_diagnostic.CRR0030.severity = error
843843
# CRR0033: The void async method should be in a try/catch block
844844
dotnet_diagnostic.CRR0033.severity = error
845845

846+
# CRR0034 - The asynchronous method should contain the "Async" suffix
847+
# duplicate with S4261
848+
dotnet_diagnostic.CRR0034.severity = none
849+
846850
# CRR0035 - No CancellationToken parameter in the asynchronous method
847851
dotnet_diagnostic.CRR0035.severity = suggestion
848852

src/Tests/ThreadingTest/Program.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Threading.Tasks;
32

43
namespace ThreadingTest;
@@ -65,9 +64,12 @@ private static async Task Main(string[] args)
6564
// Console.WriteLine($"ThreadCount: {ThreadPool.ThreadCount}");
6665
//}
6766

68-
await new AsyncLocalTest().AsyncLocal_TestAsync(default);
69-
Console.WriteLine();
70-
await new AsyncLocalTest().Wrapper_TestAsync(default);
67+
//await new AsyncLocalTest().AsyncLocal_TestAsync(default);
68+
//Console.WriteLine();
69+
//await new AsyncLocalTest().Wrapper_TestAsync(default);
70+
71+
await TaskFactoryTest.StartNewUnwrappedTest1();
72+
await TaskFactoryTest.StartNewUnwrappedTest2();
7173
}
7274

7375
#endregion
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace ThreadingTest;
7+
8+
[SuppressMessage("Async/await", "CRR0038:CancellationToken parameter is never used.", Justification = "<Pending>")]
9+
[SuppressMessage(
10+
"Minor Code Smell",
11+
"S4261:Methods should be named according to their synchronicities",
12+
Justification = "<Pending>")]
13+
public static class TaskFactoryExtensions
14+
{
15+
16+
#region Constants & Statics
17+
18+
public static Task<TResult> StartNewUnwrapped<TResult>(this TaskFactory factory, Func<Task<TResult>> function)
19+
{
20+
var task = factory.StartNew(function);
21+
22+
return task.Unwrap();
23+
}
24+
25+
public static Task<TResult> StartNewUnwrapped<TResult>(
26+
this TaskFactory factory,
27+
Func<Task<TResult>> function,
28+
CancellationToken cancellationToken)
29+
{
30+
var task = factory.StartNew(function, cancellationToken);
31+
32+
return task.Unwrap();
33+
}
34+
35+
public static Task<TResult> StartNewUnwrapped<TResult>(
36+
this TaskFactory factory,
37+
Func<Task<TResult>> function,
38+
TaskCreationOptions creationOptions)
39+
{
40+
var task = factory.StartNew(function, creationOptions);
41+
42+
return task.Unwrap();
43+
}
44+
45+
public static Task<TResult> StartNewUnwrapped<TResult>(
46+
this TaskFactory factory,
47+
Func<object?, Task<TResult>> function,
48+
object? state)
49+
{
50+
var task = factory.StartNew(function, state);
51+
52+
return task.Unwrap();
53+
}
54+
55+
public static Task<TResult> StartNewUnwrapped<TResult>(
56+
this TaskFactory factory,
57+
Func<object?, Task<TResult>> function,
58+
object? state,
59+
CancellationToken cancellationToken)
60+
{
61+
var task = factory.StartNew(function, state, cancellationToken);
62+
63+
return task.Unwrap();
64+
}
65+
66+
public static Task<TResult> StartNewUnwrapped<TResult>(
67+
this TaskFactory factory,
68+
Func<object?, Task<TResult>> function,
69+
object? state,
70+
TaskCreationOptions creationOptions)
71+
{
72+
var task = factory.StartNew(function, state, creationOptions);
73+
74+
return task.Unwrap();
75+
}
76+
77+
public static Task<TResult> StartNewUnwrapped<TResult>(
78+
this TaskFactory factory,
79+
Func<Task<TResult>> function,
80+
TaskScheduler scheduler,
81+
CancellationToken? cancellationToken = null,
82+
TaskCreationOptions? creationOptions = null)
83+
{
84+
var task = factory.StartNew(
85+
function,
86+
cancellationToken ?? factory.CancellationToken,
87+
creationOptions ?? factory.CreationOptions,
88+
scheduler);
89+
90+
return task.Unwrap();
91+
}
92+
93+
public static Task<TResult> StartNewUnwrapped<TResult>(
94+
this TaskFactory factory,
95+
Func<object?, Task<TResult>> function,
96+
object? state,
97+
TaskScheduler scheduler,
98+
CancellationToken? cancellationToken = null,
99+
TaskCreationOptions? creationOptions = null)
100+
{
101+
var task = factory.StartNew(
102+
function,
103+
state,
104+
cancellationToken ?? factory.CancellationToken,
105+
creationOptions ?? factory.CreationOptions,
106+
scheduler);
107+
108+
return task.Unwrap();
109+
}
110+
111+
#endregion
112+
113+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace ThreadingTest;
5+
6+
public static class TaskFactoryTest
7+
{
8+
9+
#region Constants & Statics
10+
11+
public static async Task StartNewUnwrappedTest1()
12+
{
13+
static Task<int> ReturnIntFunction()
14+
{
15+
return Task.FromResult(42);
16+
}
17+
18+
var result = await Task.Factory.StartNewUnwrapped(ReturnIntFunction);
19+
20+
Console.WriteLine(result);
21+
}
22+
23+
public static async Task StartNewUnwrappedTest2()
24+
{
25+
static async Task<int> TaskFunction(object? i)
26+
{
27+
await Task.Yield();
28+
return 32;
29+
}
30+
31+
var result = await Task.Factory.StartNewUnwrapped(TaskFunction, "32");
32+
Console.WriteLine(result);
33+
}
34+
35+
#endregion
36+
37+
}

template/src/.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,10 @@ dotnet_diagnostic.CRR0030.severity = error
843843
# CRR0033: The void async method should be in a try/catch block
844844
dotnet_diagnostic.CRR0033.severity = error
845845

846+
# CRR0034 - The asynchronous method should contain the "Async" suffix
847+
# duplicate with S4261
848+
dotnet_diagnostic.CRR0034.severity = none
849+
846850
# CRR0035 - No CancellationToken parameter in the asynchronous method
847851
dotnet_diagnostic.CRR0035.severity = suggestion
848852

0 commit comments

Comments
 (0)