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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:04:29+00:00 2026-06-17T20:04:29+00:00

I’m working on an app which spawns multiple threads on user-customizable intervals in order

  • 0

I’m working on an app which spawns multiple threads on user-customizable intervals in order to retrieve data from {insert any kind of service here}. My app uses CMD scripts (i.e. makes Windows spawn conhost) and sometimes mapping network drives, then they sleep for a customized interval and repeat. Let’s call them Counters.

So far so good, but there are some procedures that can’t be ran asynchronously (mapping drives, for instance), so I must make some kind of queues for some specific threads so the program works alright.

So far I’ve been able to implement it using this producer/consumer queue blog post found here -> http://moazzam-khan.com/blog/?p=418. This works in the CMD counters so the app doesn’t spawn like 7821678321 conhosts and blocks the PC, I’m queueing them. Anyways, that’s not what I’m having problems with.

Problems arise when I need to map network drives to run some commands and unmap it, then let the next thread execute. Let me illustrate the problem more clearly:

Thread Type A: Only need to be queued (only A-types), no need to wait for B-type threads to execute or finish their execution. This is done actually with the code found in the tutorial.

Thread Type B: Other B-type threads need to wait for the current B-type thread to finish its execution.

I was thinking in using the same tutorial for the B-type threads, but I realized I can’t use it because the other threads keep invoking themselves until it’s unblocked, and invoking themselves would remap the drive and it won’t work (argh, I can see if the drive is mapped but not where does it map to… I could, but it’s unneccesary code I want to avoid writing).

I did my homework and went through google and even made a small app to try my excercise by using the lock keyword in C#, without success, even though the function blocks other threads, they keep calling the blocking function and I don’t want that 🙁

There’s lots of tutorials out there but none are concise and to-the-point enough to allow me to understand them. I’ve found a few questions here in SO but they’re mostly java-related, and I’m working in C# 4.0.

Well, here’s the code I wrote. If you run it you’ll notice the other threads will call the blocking function. I don’t want the function to be called until the first thread which called it finishes. Either that, or make some kind of queue but also avoid the queue to fill itself due to the time other threads took in doing their process.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadTest
{
    public class BlockObj
    {
        Object lockObj = new Object();

        public void DoBlock(int id)
        {
            lock (lockObj)
            {
                Console.WriteLine("Counter " + id + " locking object created, wait 5 sec");
                Thread.Sleep(5000);
                Console.WriteLine("Stuff done, keep going");
            }
        }
    }

    class Program
    {
        static Random r = new Random(DateTime.Now.Second);
        static BlockObj oo = new BlockObj();

        public static void Counter(object id)
        {
            int i = (int)id;
            while (true)
            {
                Thread.Sleep(1000 * r.Next(1, 10));
                Console.WriteLine("Counter " + i + " call blocking stuff...");
                oo.DoBlock(i);
                Console.WriteLine("Counter " + i + " finish work");
            }
        }

        public static void Main(string[] args)
        {
            for (int i = 0; i < 5; i++)
            {
                Thread t = new Thread(new ParameterizedThreadStart(Counter));
                t.Start(i);
            }
        }
    }
}

Actually, I’d like to see some proof-of-concept code of an app that would spawn two thread queues, and the threads in those queues would wait for the current thread to finish executing before running the next one. However, each queue would have their own blocking. Thread Queue 1 shouldn’t block Thread Queue 2 executions and visceversa.

  • 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-17T20:04:32+00:00Added an answer on June 17, 2026 at 8:04 pm

    Okay, I just solved it with my own code, trying stuff until it worked :).

    For those who want a synchronized, simple queue, here it is:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Collections;
    using System.Runtime.Remoting.Messaging;
    
    namespace TheadingPool1
    {
        // http://www.c-sharpcorner.com/UploadFile/mgold/QueueInThread11222005232529PM/QueueInThread.aspx
    
        public class ThreadQueue
        {
            private Queue _qOrder = new Queue();
            private Queue _qSync;
            private Thread _qThread;
            private ManualResetEvent _eWait = new ManualResetEvent(false);
    
            private int _wait;
    
            public ThreadQueue(int w)
            {
                _wait = w;
                _qSync = Queue.Synchronized(_qOrder);
            }
    
            public void Start()
            {
                _qThread = new Thread(new ThreadStart(Process));
                _qThread.IsBackground = true;
                _qThread.Start();
            }
    
            public void Process()
            {
                Random x = new Random(DateTime.Now.Second);
                object item;
                while (true)
                {
                    item = null;
                    lock (_qSync.SyncRoot)
                    {
                        if (_qSync.Count > 0)
                        {
                            item = (object)_qSync.Dequeue();
                        }
                        else
                        {
                            _eWait.Reset();
                        }
                    }
                    if (item != null)
                    {
                        Console.WriteLine("[" + _wait + "] [" + item.ToString() + "] :D");
                        Thread.Sleep(_wait * x.Next(1, 5));
                    }
                    else
                    {
                        _eWait.WaitOne();
                    }
                }
            }
    
            public void Enqueue(object obj)
            {
                _qSync.Enqueue(obj);
                _eWait.Set();
            }
        }
    
        public class Program
        {
            private static ThreadQueue _q1 = new ThreadQueue(10);
            private static ThreadQueue _q2 = new ThreadQueue(50);
    
            public static void Main(string[] args)
            {
                _q1.Start();
                _q2.Start();
    
                for (int i = 0; i < 50; i++)
                    _q1.Enqueue(i);
    
                for (int i = 0; i < 50; i++)
                    _q2.Enqueue(i);
    
                while (true)
                {
                    Thread.Sleep(100);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I am writing an app for my school newspaper, which is run completely online
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.