如何异步排队(串)从一个线程的消息到另一个.NET 2.0 / 3.5?线程、消息、NET

2023-09-07 03:58:54 作者:妩媚狐狸-A.

我就找到了我想要实现在C# Queue实现文章。但文章是基于.NET 4.0。和MSDN网站上有很简单的例子而已。因此决定发布此位置。

I found the article regarding what I want to implement at Queue implementation in C#. But the article is based on .Net 4.0. and MSDN website has very simple example only. so decided to post this here.

这就是我想实现一个线程从队列中取字符串中的DOS提示符窗口中显示的字符串是异步的。另一个线程可能在字符串队列中添加额外的字符串异步以及(字符串最大项是20,一旦填写最大项,没有更多的补充)。我想运行该应用程序,直到基于.NET 2.0或3.5队列为空。(VS2008)

That is, I'd like to implement that a thread displays a string asynchronously in dos prompt window by taking the string from a queue. Another thread might add additional strings in the string queue asynchronously as well(string maximum entry is 20, once fill maximum entry, no more addition). And I'd like to run this application until the queue is empty based on .Net 2.0 or 3.5.(VS2008)

队列实现看上去简单,因为.Net提供了使用它,但我不知道如何实现这个队列中的异步部分。

Queuing implementation looks simple because .Net provides it, but I don't know how to implement asynchronous part with this queue.

感谢你。

推荐答案

所以我收集的是你想基本上是使用 ConcurrentQueue 但你停留在.NET 3.5和不能使用它(因为它不存在)。

So what I gather is you want to use essentially the ConcurrentQueue but you're stuck in .NET 3.5 and cannot use it (because it doesn't exist).

这又回到了简单的操作锁定。不希望在同一时间访问相同的对象两个线程

This goes back to simple operation locking. You don't want two threads accessing the same object at the same time.

开始之前:请阅读本书对C#线程由约瑟夫阿尔巴哈利

据越过.NET线程,甚至只是撇去它会帮助你运行到坏的线程错误prevent。

It goes over .NET Threading and even just skimming it will help you prevent running into bad threading mistakes.

有两件事情,你基本上需要做的,让我们打破这:

There's 2 things you essentially need to do, so let's break this down:

您可以使用 通用队列< T>类 来实现你的消息队列,然而的安全处理需要能够改变这个对象的所有操作。

You can use the Generic Queue<T> Class to implement your message queue, however all operations that change this object need to be handled safely.

这样做的最简单的形式是使用的锁定声明。

The simplest form of doing this is using a Lock Statement.

lock(myQueue)
{
   myQueue.Enqueue(message);
}

您必须锁定所有操作,所以你应该需要三个是 .Enqueue(...) .Dequeue() .Count之间,因为它们都访问数据。

You must lock around all operations, so the three you should need are .Enqueue(...), .Dequeue() and .Count since they all access data.

这将$ P $运行到多线程问题,排队pvent你/你出队的消息,生活就会好。

This will prevent you from running into multi-threading problems with queueing/dequeuing your messages and life will be good.

要等待一个消息,你有很多方法可以解决这个问题。他们大多是在电子书我上面链接中概述。

To wait for a message you have a number of ways you can solve this. Most of them are outlined in the e-book i linked above.

最简单的是一个 Thread.sleep代码循环

while(appIsRunning)
{
    Thread.Sleep(100);
    lock(myQueue)
    {
        while(myQueue.Count > 0)
        {
           Console.WriteLine(myQueue.Dequeue());
        }
    }
}

注意:这不是做到这一点的最好办法,只是最简单的例子

这样做是简单的时间表这个线程从现在又来至少100ms的运行。 (有没有gaurantee它会从现在运行100毫秒,只是在上述时间之前)。然后,它锁定的队列所以没有写可能发生,它清空队列,行写到屏幕然后循环一次。

What this does is simply schedules this thread to run again at least 100ms from now. (There's no gaurantee it'll run 100ms from now, just not before that time). Then it locks the queue so no writes can happen to it, it empties the queue and writes the lines to the screen then loops again.

如果队列为空,它会只是回去睡觉。

If the queue is empty it'll just go back to sleeping.

另一种解决方案是采用脉冲/等待

The other solution is using a Pulse/Wait

Monitor.Pulse Monitor.Wait 是一个乒乓式的线程控制模式。

Monitor.Pulse and Monitor.Wait are a ping-pong style thread control paradigm.

您可以在主线程 Monitor.Wait()你的循环,当你添加一条消息到队列您可以 Monitor.Pulse ()以解除锁定它是一个等待的线程。

You can have your loop in the main thread Monitor.Wait() and when you add a message to the queue you can Monitor.Pulse() to release the lock it's to a waiting thread.

这是语义上更相关,但可能不太有效,因为你的上下文切换是发生在每封邮件的需求。

This is more semantically relevant but probably less efficient because your context switch is happening on demand for each message.

还有约10名其他的方法可以做到这一点,大部分都在这本书中列出但本质上它的JIST。

There's about 10 other ways to do this, most are outlined in that book but that's essentially the jist of it.

请参阅约瑟夫的书事件信令部分对所有的信令的例子的方法。

Please see Event Signaling Section of Joseph's Book for examples of all the signaling methods.