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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:39:12+00:00 2026-05-30T17:39:12+00:00

Currently I have multiple resources in a project that can only be accessed from

  • 0

Currently I have multiple resources in a project that can only be accessed from a single thread. One example is a ZeroMQ server and another is a Mitsubishi PLC communication module that’s not threadsafe.

Right now I create a seperate thread for any resource requiring single-threaded access. I haven’t wrapped this in a class or anything, so I have a ThreadStart (void) method and a couple of properties in every class requiring coordinated access to a resource. I then use a dispatcher combined with Invoke / BeginInvoke to force access from the seperate Thread only.

Is there some class already available that can coordinate access to a resource from a single thread?

Here’s some sample code that will get me a Dispatcher right now:

object theLock = new object();
Dispatcher dispatcher = null;

lock (theLock)
{
    new Thread(new ThreadStart(() =>
    {
        lock (theLock)
        {
            dispatcher = Dispatcher.CurrentDispatcher;
            Monitor.Pulse(theLock);
        }
        Dispatcher.Run();
    })).Start();

    Monitor.Wait(theLock);
}

dispatcher.Invoke(...);

I can wrap this code in a class just to create a thread and dispatcher whenever I need one, but I can’t imagine this being the best solution to coordinate access to a thread-unsafe resource.

  • 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-30T17:39:14+00:00Added an answer on May 30, 2026 at 5:39 pm

    Did you try using Task and BlockingCollection? You could enqueue in there tasks that need to access the resource, and start a task that dequeue and run the task, one after each others so there is no race condition?

    static BlockingCollection<Action<StringBuilder>> queue = new BlockingCollection<Action<StringBuilder>>();
    
    //Your thread unsafe resource...
    static StringBuilder resource = new StringBuilder();
    
    static void Main(string[] args)
    {
        Task.Factory.StartNew(() =>
        {
            while (true)
            {
                var action = queue.Take();
                action(resource);
            }
        });
    
        //Now to do some work you simply add something to the queue...
        queue.Add((sb) => sb.Append("Hello"));
        queue.Add((sb) => sb.Append(" World"));
    
        queue.Add((sb) => Console.WriteLine("Content: {0}", sb.ToString()));
    
        Console.ReadLine();
    }
    

    It’s all static and a bit ugly and certainly needs rework, but it should be ok. The idea is that you enqueue lambdas that you want to execute, and they will be executed one after each other so you don’t have to synchronize on the resource.

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

Sidebar

Related Questions

I'm currently managing multiple models from a single SITE MANAGER page. I have the
I am currently working on a project with multiple ejb and one ejb-jar.xml. I
I currently have an ASP.net 2.0 site with multiple ASP.Net 1.1 sites running as
I currently have an iPhone application with a tabbar and multiple viewcontrollers. All the
If you install multiple versions of python (I currently have the default 2.5, installed
I'm having an issue with multiple markers on google maps - I currently have
I currently have an MS Access application that connects to a PostgreSQL database via
I'm currently using PHP to include multiple css (or js) files into a single
Is there a Windows (or .NET) synchronization primitive that: can be shared across multiple
I'm currently working on a project where we have a lot of dependencies. I

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.