Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 5 additions & 34 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ NuGet package: https://www.nuget.org/packages/ContextFreeTasks/
Use `ContextFreeTask` struct instead of `Task` class (`System.Threading.Tasks` namespace) for return types of async methods.

```csharp
private async ContextFreeTask FAsync()
{
...
}
private async ContextFreeTask FAsync() { ... }
private async ContextFreeTask<T> FAsync<T>() { ... }
```

This ignores the current synchronization context.
These ignore the current synchronization context in the methods.
You do not have to write `ConfigureAwait(false)` anymore.

### Task-like
Expand All @@ -25,11 +23,7 @@ The `ContextFreeTask` struct in this library satisfies the "Task-like" pattern a

### What ContextFreeTask does

This struct ignores synchronization context at all.
In other words,

- `await` operations in the methods which return `ContextFreeTask` capture no context
- `await` operations on `ContextFreeTask` capture no context
The methods which return `ContextFreeTask` do not capture the synchronization context.

For example, you can use this as following:

Expand All @@ -43,14 +37,6 @@ private async ContextFreeTask FAsync()
await Task.Delay(100);
// no context here
}

private async Task GAsync()
{
// You can await on ContextFreeTask
// ContextFreeTask don't capture current context
await FAsync();
// no context here
}
```

This code behaves almost the same as the following:
Expand All @@ -61,11 +47,6 @@ private async Task FAsync()
await Task.Delay(100).ConfigureAwait(false);
await Task.Delay(100).ConfigureAwait(false);
}

private async Task GAsync()
{
await FAsync().ConfigureAwait(false);
}
```

### Implementation
Expand All @@ -88,17 +69,7 @@ public struct ContextFreeTask<T>
}
```

Those awaiters always use `ConfigureAwait(false)`.

```cs
public struct ContextFreeTaskAwaiter : ICriticalNotifyCompletion
{
private readonly Task _value;
public void OnCompleted(Action continuation) => _value.ConfigureAwait(false).GetAwaiter().OnCompleted(continuation);
}
```

Furthermore, those AsyncMethodBuilders always clear current synchronization context.
Those AsyncMethodBuilders always clear current synchronization context.

```cs
public struct AsyncContextFreeTaskMethodBuilder
Expand Down
2 changes: 1 addition & 1 deletion src/ContextFreeTasks.Shared/ContextFreeTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct ContextFreeTask
private readonly Task _task;
public Task Task => _task ?? FromResult(default(object));
internal ContextFreeTask(Task t) => _task = t;
public ContextFreeTaskAwaiter GetAwaiter() => new ContextFreeTaskAwaiter(Task);
public TaskAwaiter GetAwaiter() => Task.GetAwaiter();
public void Wait() => _task?.Wait();
public ConfiguredTaskAwaitable ConfigureAwait(bool continueOnCapturedContext) => Task.ConfigureAwait(continueOnCapturedContext);
}
Expand Down
16 changes: 0 additions & 16 deletions src/ContextFreeTasks.Shared/ContextFreeTaskAwaiter.cs

This file was deleted.

16 changes: 0 additions & 16 deletions src/ContextFreeTasks.Shared/ContextFreeTaskAwaiter_T.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/ContextFreeTasks.Shared/ContextFreeTask_T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct ContextFreeTask<T>
public Task<T> Task => _task ?? FromResult(default(T));
public T Result => GetAwaiter().GetResult();
internal ContextFreeTask(Task<T> t) => _task = t;
public ContextFreeTaskAwaiter<T> GetAwaiter() => new ContextFreeTaskAwaiter<T>(Task);
public TaskAwaiter<T> GetAwaiter() => Task.GetAwaiter();
public void Wait() => _task?.Wait();
public ConfiguredTaskAwaitable<T> ConfigureAwait(bool continueOnCapturedContext) => Task.ConfigureAwait(continueOnCapturedContext);
}
Expand Down
12 changes: 7 additions & 5 deletions src/ContextFreeTasks.Shared/ContextFreeTasks.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
<Import_RootNamespace>ContextFreeTasks</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)AsyncContextFreeTaskMethodBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)AsyncContextFreeTaskMethodBuilder_T.cs" />
<Compile Include="$(MSBuildThisFileDirectory)AsyncMethodBuilderAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Internal\AsyncContextFreeTaskMethodBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Internal\AsyncContextFreeTaskMethodBuilder_T.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System.Runtime.CompilerServices\AsyncMethodBuilderAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ContextFreeTask.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ContextFreeTaskAwaiter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ContextFreeTaskAwaiter_T.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ContextFreeTask_T.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)Internal\" />
<Folder Include="$(MSBuildThisFileDirectory)System.Runtime.CompilerServices\" />
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions src/ContextFreeTasks.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>ContextFreeTasks</id>
<version>0.2.0</version>
<version>0.3.0</version>
<title>ContextFreeTasks</title>
<authors>Nobuyuki Iwanaga</authors>
<owners>Nobuyuki Iwanaga</owners>
<licenseUrl>https://github.com/ufcpp/ContextFreeTask/blob/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/ufcpp/ContextFreeTask/</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Task-like structs which capture no synchronization context on the await operations in C#</description>
<releaseNotes>Add and change some methods for consistency with the Task and the ValueTask</releaseNotes>
<releaseNotes>remove context-free awaiters. ContextFreeTask provides only context-free method-builders.</releaseNotes>
<copyright>Nobuyuki Iwanaga</copyright>
<tags>Task-like, async method, ConfigureAwait, SynchronizationContext</tags>
<tags>Task-like, async method, ConfigureAwait, SynchronizationContext, AsynchMethodBuilder</tags>
<frameworkAssemblies>
<frameworkAssembly assemblyName="System" targetFramework="net45" />
</frameworkAssemblies>
Expand Down
6 changes: 3 additions & 3 deletions tests/UnitTestNet45/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ private async Task<string> A1(int n)
{
OnMainThread();
var s = await A2(n);
OnPoolThread();
OnMainThread();
await Task.Delay(100);
OnPoolThread();
OnMainThread();
await A3();
OnPoolThread();
OnMainThread();
return s;
}

Expand Down