Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
C#

C#: Fix a Custom Awaiter Resuming on the Wrong Thread Context

- 25.11.26 - ErcanOPAK

đź”§ C#: Fix a Custom Awaiter Resuming on the Wrong Thread Context

A hand-rolled custom awaitable has to replicate the thread-context capturing that Task‘s own awaiter already does for you — if its OnCompleted method just stores the continuation and invokes it directly from whatever background callback completes the operation, instead of capturing SynchronizationContext.Current and posting the continuation back through it, every await on that awaitable effectively becomes ConfigureAwait(false) whether the caller wanted that or not.

🔎 The Problem

public void OnCompleted(Action continuation)
{
    _continuation = continuation;
    // later, called from a background I/O thread:
    _continuation?.Invoke();   // resumes on whatever thread completed the op,
}                              // never the original calling context

âś… Fix: Capture the Context in OnCompleted and Post Back Through It

  • Capture SynchronizationContext.Current inside OnCompleted, at the moment the continuation is registered, and post the continuation back through that captured context when the operation completes, exactly like the built-in task awaiters do.
  • If the custom awaitable is always meant to behave like ConfigureAwait(false), document that explicitly on the type, since a caller awaiting it has no other way to know that guarantee doesn’t exist here.
  • Write a test that awaits the custom awaitable from a context with a non-null SynchronizationContext and asserts the continuation actually resumes on it, rather than only testing from a console app’s default context, where the difference is invisible.

🚨 Why This Is Dangerous

  • UI code that calls into a custom awaitable and assumes it’s back on the UI thread afterward, because every other await in the codebase behaves that way, can throw a cross-thread access exception at the exact line that touches a control — or silently corrupt UI state without throwing at all.

Task’s awaiter remembers where you called it from so you don’t have to — write your own awaiter and that memory is suddenly your job.

— C# Best Practice

Related posts:

C#: Fix a Required Member Compiling Without a Setter Call

C#: Fix a Required Member Compiling Without a Setter Call

C#: Fix String Interpolation That Silently Uses the Wrong Culture Because ToString() Was Called With...

C#: Fix String Interpolation That Silently Uses the Wrong Culture Because ToString() Was Called With...

ASP.NET Core “Slow Startup” — Reflection Scanning Trap

ASP.NET Core “Slow Startup” — Reflection Scanning Trap

Views: 12
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Docker: Fix a Container’s DNS Resolving a Name It Already Left
Git: Fix an Alias Expanding Differently Inside a CI Runner

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com