GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - ufcpp/ContextFreeTask: Task-like structs which capture no synchronization context on the await operations.

BranchesTags
Open more actions menu

Latest commit

 

History

50 Commits
50 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
src/ContextFreeTasks
src/ContextFreeTasks
 
 
tests/UnitTestNet45
tests/UnitTestNet45
 
 
.gitignore
.gitignore
 
 
ContextFreeTask.sln
ContextFreeTask.sln
 
 
LICENSE
LICENSE
 
 
readme.md
readme.md
 
 

Repository files navigation

ContextFreeTask

Usage

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.

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

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

Task-like

In C# 7, async methods may return other types in addition to Task, Task<T> and void. The returned type must satisfy a certain pattern. These types are called "Task-like".

The ContextFreeTask struct in this library satisfies the "Task-like" pattern and the "awaitable" pattern.

What ContextFreeTask does

The methods which return ContextFreeTask do not capture the synchronization context.

For example, you can use this as following:

// All awaits in this method don't capture SynchronizationContext
private async ContextFreeTask FAsync()
{
    // Whatever current context is
    await Task.Delay(100);
    // no context here
    await Task.Delay(100);
    // no context here
}

This code behaves almost the same as the following:

private async Task FAsync()
{
    await Task.Delay(100).ConfigureAwait(false);
    await Task.Delay(100).ConfigureAwait(false);
}

Implementation

The ContextFreeTask struct is a thin wrapper of Task class.

public struct ContextFreeTask
{
    public Task Task { get; }
}

And the ContextFreeTask<T> is that of Task<T>.

public struct ContextFreeTask<T>
{
    public Task<T> Task { get; }
}

Those AsyncMethodBuilders always clear current synchronization context.

public struct AsyncContextFreeTaskMethodBuilder
{
    private AsyncTaskMethodBuilder _methodBuilder;

    public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
        where TAwaiter : INotifyCompletion
        where TStateMachine : IAsyncStateMachine
    {
        var prevContext = SynchronizationContext.Current;
        try
        {
            SynchronizationContext.SetSynchronizationContext(null);
            _methodBuilder.AwaitOnCompleted(ref awaiter, ref stateMachine);
        }
        finally
        {
            SynchronizationContext.SetSynchronizationContext(prevContext);
        }
    }
}

About

Task-like structs which capture no synchronization context on the await operations.

Resources

Readme
MIT license
Activity

Stars

40 stars

Watchers

6 watching

Forks

6 forks
Report repository

Releases

Sponsor this project

Packages

Used by

Contributors

Languages

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.