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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:10:00+00:00 2026-06-17T15:10:00+00:00

I want to create a thread-safe method I can call from several workers without

  • 0

I want to create a thread-safe method I can call from several workers without blocking the worker threads. I also want the code to be decently brief much like the InvokeRequired->BeginInvoke type code is for UI Controls… of course I’m not using a Control in this case, the method is on a custom class. I also don’t necessarily want this for every method. So is this something that can be easily implemented on custom classes/methods?

Sudo Code:

class foo
{
    //Fields
    Context MyContext;
    List<T> UnsafeList = new List<T>();

    //Method
    public void MyMethod(int someArg, Item someOtherArg)
    {
        if (!MyContext)
        {
            MyContext.BeginInvoke(...);
            return; //Calling thread returns
        }
        else
        {
            UnsafeList.Add(someOtherArg);
            return; //MyContext Thread returns
        }
    }

    //Constructor
    public foo()
    {
        MyContext = new GetSomeThreadHandle();
    }
}

I think I have an idea of how to do this with custom EventArgs, but it involves nearly repetitive code and I was looking for a more generic/cleaner solution.

Edit

This is using C# .Net 4.0

  • 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-06-17T15:10:01+00:00Added an answer on June 17, 2026 at 3:10 pm

    Given that:

    • You’re using .NET 4
    • You mentioned queuing and serializing the requests…

    … it sounds like you want a producer/consumer queue, via BlockingCollection<T>. Start one thread (a consumer) which will pull from the queue, and then just add to the queue from your worker threads.

    You’ll need to think about what you want to happen if the queue builds up to an unexpected/undesirable level. (You could block, you could throw an exception, you could drop the requests.)

    EDIT: For more information, see this MSDN blog post on BlockingCollection<T>.

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

Sidebar

Related Questions

I want to create a thread-safe collection that can be modified while being enumerated.
I want to create a Thread safe JSP page. It is possible in Servlet
I want to create a worker thread that isn't the main thread so I
when i create a thread, i want to pass several arguments. So i define
I want to create a non-thread-safe chunk of code for experimentation, and those are
Let's say I want to create a collection class that is thread-safe by default.
I am trying to create thread safe properties in C# and I want to
I want to create a background thread that's owned by an object. When that
I want to create a separate thread that runs its own window. Frankly, the
I want to create dynamic number of thread which is depends on the database

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.