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.

4

1 回答 1

0

好的,我只是用自己的代码解决了它,尝试了一些东西直到它起作用:)。

对于那些想要一个同步的简单队列的人,这里是:

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);
            }
        }
    }
}
于 2013-01-24T16:04:59.117 回答