Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7691555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:39:07+00:00 2026-05-31T20:39:07+00:00

I am trying to discover how the ExecutionContext actually works in version 4.0 and

  • 0

I am trying to discover how the ExecutionContext actually works in version 4.0 and above of the .NET Framework. The documentation says that the managed principle, synchronization, locale and user context all flow to the new thread when using Thread.Start and most thread pool operations. But I cannot see this working at all in practice.

Here is a simple console application that tests if the synchronization context and managed principle flow when starting a new thread…

    static void Main(string[] args)
    {
        SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("One"), null);

        Thread t1 = new Thread(new ThreadStart(ThreadRun));
        t1.Start();
        t1.Join();

        SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("Two"), null);

        AsyncFlowControl aFC = ExecutionContext.SuppressFlow();
        Thread t2 = new Thread(new ThreadStart(ThreadRun));
        t2.Start();
        t2.Join();
        aFC.Undo();

        Console.Read();
    }

    static void ThreadRun()
    {
        Console.WriteLine("ThreadRun Id={0} Context={1} Principle={2}", 
            Thread.CurrentThread.ManagedThreadId, 
            (SynchronizationContext.Current != null), 
            Thread.CurrentPrincipal.Identity.Name);
    }

The result is…

    ThreadRun Id=11 Context=False Principle=One
    ThreadRun Id=12 Context=False Principle=Two

So the synchronization context never flows and the managed principle always flows even when you specify it should not. Basically the documentation is completely wrong. So is there a description of what ExecutionContext does in reality and why it is useful?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:39:08+00:00Added an answer on May 31, 2026 at 8:39 pm

    That’s pretty misleading documentation. I can’t answer the broader thrust of your question, but I can tell you why SynchronizationContext does’t flow.

    If you look at the source of Thread.Start, it eventually calls down to:

        [SecuritySafeCritical]
        private void Start(ref StackCrawlMark stackMark)
        {
          this.StartupSetApartmentStateInternal();
          if (this.m_Delegate != null)
            ((ThreadHelper) this.m_Delegate.Target).SetExecutionContextHelper(ExecutionContext.Capture(ref stackMark, ExecutionContext.CaptureOptions.IgnoreSyncCtx));
          this.StartInternal(CallContext.Principal, ref stackMark);
        }
    

    Note that it explicitly passes ExecutionContext.CaptureOptions.IgnoreSyncCtx by default. It also passes CallContext.Principal regardless of ExecutionContext.SuppressFlow(). So, the explains why you are seeing what you are seeing, but not when it might be useful or why the docs are flat out wrong!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to discover items that a pubsub service provides. When I log
I was profiling my .net windows service. I was trying to discover OutOfMemoryException and
I am trying to extend the LightOpenID PHP library to discover that an identity
If I have significant amounts of text and am trying to discover templates that
I'm trying to list some JmDNS services that I discover in my Android app
In this particular case I'm trying to discover if a mylib.a file is 32
Greetings, I was trying to discover a proper way to send captured errors or
I am trying to use wireshark to discover why visual studio is not downloading
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
When trying to discover something in the code of some open source C projects,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.