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 537199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:54:06+00:00 2026-05-13T09:54:06+00:00

So I’m writing a bunch or multithreaded code in library that will run in

  • 0

So I’m writing a bunch or multithreaded code in library that will run in the background. So I’m having the UI pass in SynchronizationContext object so I can Post events back to the main UI thread.

Many different objects have public events the UI can subscribe to. I initially just add the SyncContext object as a parameter to the creation call the sort of initializing my library. Once passed in I store it in a static variable on a static class thus making it globally accessible (although it is internal).

I like this because anytime I need to make some bit of code run on the UI thread I can easily do it without modifing much code. But it means that alot of my code has a dependancy on the Static class that isn’t explicit. Making the dependency explicit would require alot of code change to add it to the constructor of any class that uses the variable, storing a reference to it in any class that even creates objects that need it.

So i have option

A) static variable that hides dependencies for my SynchronizationContext

B) nearly every class knows about and passes around my SynchronizationContext.

Is there another pattern that would be better for this sort of situation?

.net framwork 3.5. The main UI is going to be WPF, but we want it to work with winforms as well.

  • 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-13T09:54:06+00:00Added an answer on May 13, 2026 at 9:54 am

    If you know that your library will only ever be used from a single synchronization context, then I see no reason why you can’t capture the synchronization context using a static member (your option A).

    Why do you see a need to make this dependency ‘explicit’?

    Where option A could potentially be problematic is if your library may be used from multiple synchronization contexts at the same time. (For example multiple UI windows running on their own thread and message pump). This is fairly unusual though – mostly you have a single UI thread and a single synchronization context.

    Also, if an application wishes to handle some events without forcing serialization to the UI thread (via the sync context) it cannot do so.

    If these issues are not a problem for your library, then option A should be viable.

    Edit – in response to comment:

    Joel, I’m not sure if this is helpful given what you describe, but one thing you might want to consider if you sometimes want to use an explicit synchronization context would be to use thread-local storage to store the parameter without needing to create overrides of the method to take the parameter.

    For example, I have in the past myself needed to allow callers to my APIs to both use the default synchronization context for the current calling thread but also to explicitly set a different synchronization context. One approach I could have taken would have been to provide overloads of my API methods that allow passing a synchronization context parameter. In my case, this would have been pretty ugly since it would have meant creating a lot of method overloads for a fairly infrequent use case.

    So, what I did instead was create a class that handled creating a ‘scope’ during which the current sync context gets overridden (thereby effectively passing it to the called methods). The class takes care of (in order) 1) caching the current sync context, 2) setting the current context to a sync context specified by the caller, and 3) resetting the sync context at the end of the ‘scope’.

    Here’s what it looks like:

    public class SyncContextScope : IDisposable
    {
        SynchronizationContext m_contextOnEntry;
    
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="useContext"></param>
        public SyncContextScope(SynchronizationContext useContext)
        {
            m_contextOnEntry = SynchronizationContext.Current;
            SynchronizationContext.SetSynchronizationContext(useContext);
        }
    
        #region IDisposable Members
    
        void IDisposable.Dispose()
        {
            SynchronizationContext.SetSynchronizationContext(m_contextOnEntry);
        }
    
        #endregion
    }
    

    And you use it like so:

    using(new SyncContextScope(yourSyncContext))
    {
       yourApi.CallSomeMethod();
    }
    

    Your API’s method (in the above example CallSomeMethod) can now make use of the SynchronizationContext.Current (which uses TLS to store a sync context). All without having to resort to providing a sychronizationcontext parameter.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.