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

  • Home
  • SEARCH
  • 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 6101179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:28:14+00:00 2026-05-23T13:28:14+00:00

I have a multi-threaded environment in .NET that needs to communicate to a library.

  • 0

I have a multi-threaded environment in .NET that needs to communicate to a library. The library has some limitations and therefore cannot work correctly with multi-threaded environments. I need a way to solve this issue by making middle layer that the multi-threaded environment communicates with and then calls the library. I made a solution that solved the problem but I am not convinced that it is the best one. I created an asynchronous thread that has a run method. The run method is in infinite loop that checks what is the current task (a parameter that I set) and executes the corresponding method.

As I said, I am not convinced at all with this solution but it does the job correctly. So please inform me if there is a better one to solve it (especially if it is related to C#).

Just as a side note, the limitation isn’t due to multiple threads that are working at the same time and causing data corruption. I need the exact same thread to do everything (depends on thread id).

  • 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-23T13:28:15+00:00Added an answer on May 23, 2026 at 1:28 pm

    Have a look at the Task Parallel Library.

    I recommend you implement a custom TaskScheduler that schedules all tasks on the same thread. You can use the scheduler to schedule a Task (no result value) or a Task<T> (result of type T).

    You can use a BlockingCollection<Task> wrapping a ConcurrentQueue<Task> to implement the scheduler.

    For example, assuming that your library exposes a function MakeCoffee, you could create a class CoffeeMachine that provides a matching wrapper method and schedules the task of calling the library on the custom scheduler:

    public class CoffeeMachine
    {
        private readonly TaskScheduler scheduler;
    
        public CoffeeMachine()
        {
            this.scheduler = new SingleThreadTaskScheduler();
        }
    
        public Task<Coffee> MakeCoffeeAsync()
        {
            return Task.Factory.StartNew<Coffee>(
                this.MakeCoffee,
                CancellationToken.None,
                TaskCreationOptions.None,
                this.scheduler);
        }
    
        private Coffee MakeCoffee()
        {
            // this method will always execute on the same thread
        }
    }
    

    Usage:

    CoffeeMachine machine = new CoffeeMachine();
    
    Task<Coffee> task = machine.MakeCoffeeAsync();   // start task
    
    task.Wait();                                     // wait for task to complete
    Drink(task.Result);                              // use result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object in a multi-threaded environment that maintains a collection of information,
I have a multi-threaded C++ app which does 3D rendering with the OpenSceneGraph library.
I have a multi-threaded Windows application that occasionally deadlocks. Inevitably this happens on a
I have a .NET (C#) multi-threaded application and I want to know if a
I have some questions about multi-threaded programming and multi-core usage. In particular I'm wondering
In a C# windows forms application. I have a splash screen with some multi-threaded
it may be basic question to have a singleton in multi-threaded environment we can
I have a multi-threaded web application with about 1000~2000 threads at production environment. I
I have a multi-threaded Windows C++ app written in Visual Studio 6. Within the
I currently have heavily multi-threaded server application, and I'm shopping around for a good

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.