如何分派工作项目,以单个线程 [英] How to dispatch work items to a single thread

查看:104
本文介绍了如何分派工作项目,以单个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起这个问题不是很清楚,如果我知道正确的词语来形容这个问题,谷歌很可能会想出答案。

Sorry this question is not very clear, if I know the correct words to describe the problem, Google would be likely to come up with the answer.

我要寻找一个队列类:

  • 允许任意数量的线程把一个项目队列
  • 的项目,在它们被添加到队列的顺序处理
  • 在我不介意线程处理项目
  • 只有一个项目在一个时间进行处理。
  • 在我宁可不要阻塞等待项目被添加到队列中,如果有在队列中没有商品的线程。
  • 正常情况下是队列是空的大部分时间。

例如,就像一个BeginInvoke的发生的事情在一个WinForms窗口...(或PostMessage的,如果你有充分的原料进行Win32编程) 我们使用的是.NET 3.5

e.g Just like what happens with a BeginInvoke on a WinForms windows... (Or PostMessage if you have every done raw win32 programming) We are using .net 3.5

我要寻找的东西现成的,在.NET框架或开源项目,它具有良好的单元测试,因为我不希望有写所有的单元测试一个家庭化妆的解决方案。

I am looking for something ready-made in the .net framework, or a open source project that has good unit tests, as I don't wish to have to write all the unit tests for a home-make solution.

有关背景信息,请参阅<一href="http://stackoverflow.com/questions/2280756/why-are-my-message-be-processed-out-of-order-over-a-single-wcf-tcp-channel-with/2332554#2332554">Why在我的邮件进行处理乱序在一个单一的WCF TCP信道(与ConcurrencyMode.Reentrant)?通过使用此depatcher我能够改变使用ConcurrencyMode.Single仍然advoid死锁的。

For background, see Why are my message be processed out of order over a single WCF TCP channel (with ConcurrencyMode.Reentrant)? by using this depatcher I was able to change to using ConcurrencyMode.Single and still advoid deadlocks.

推荐答案

下面是一个小品类,可以做到这一点:

Here's a sketch of a class that can do that:

public class WorkerQueue<T> {
    private Queue<T> workerQueue   = new Queue<T>();
    private object   padlock       = new object();
    private bool     isProcessing  = false;
    private Thread   workerThread;

    public void QueueWorkItem(T item) {
        lock(padlock) {
            workerQueue.Enqueue(item);
            if (!isProcessing) {
                isProcessing = true;
                workerThread = new Thread(() => { this.ProcessWork });
                workerThread.Start();

            }
        }
    }

    private void ProcessWork() {
        // 1) Thread-safe dequeue operation
        // 2) Keep processing while work is on the queue. External callers can
        //    add items to the queue while this is ongoing.
        // 3) When the queue is empty, set isProcessing to false (thread-safely)
    }

}

应用程序将使用这样的:

Applications would use it like this:

public class Application {
    private WorkerQueue<object> workerQueue = new WorkerQueue<object>();

    // This can run on multiple threads if need be
    public void SomeMethodThatCreatesWork() {
        object workItem = ExternalCall();
        this.workerQueue.QueueWorkItem(workItem);
    }
}

这很可能是有用的,让应用程序停止处理为好,可能是通过增加一个标志, ProcessWork 可以检查每个项目出列后,但目前还不清楚是什么做未处理的物品(也许这将是足以允许访问他们,让来电者决定)。

It would probably be useful to let applications stop the processing as well, probably by adding a flag that ProcessWork could check after each item is dequeued, but it's not clear what to do with unprocessed items (perhaps it would be enough to allow access to them and let the caller decide).

这篇关于如何分派工作项目,以单个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆