Code Monkey home page Code Monkey logo

contextfreetask's Introduction

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()
{
    ...
}

This ignores the current synchronization context. 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

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

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
}

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:

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

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 awaiters always use ConfigureAwait(false).

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.

public struct AsyncContextFreeTaskMethodBuilder
{
    private AsyncTaskMethodBuilder _methodBuilder;

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.