<bdo id='1leUl'></bdo><ul id='1leUl'></ul>

      <tfoot id='1leUl'></tfoot><legend id='1leUl'><style id='1leUl'><dir id='1leUl'><q id='1leUl'></q></dir></style></legend>

    1. <i id='1leUl'><tr id='1leUl'><dt id='1leUl'><q id='1leUl'><span id='1leUl'><b id='1leUl'><form id='1leUl'><ins id='1leUl'></ins><ul id='1leUl'></ul><sub id='1leUl'></sub></form><legend id='1leUl'></legend><bdo id='1leUl'><pre id='1leUl'><center id='1leUl'></center></pre></bdo></b><th id='1leUl'></th></span></q></dt></tr></i><div id='1leUl'><tfoot id='1leUl'></tfoot><dl id='1leUl'><fieldset id='1leUl'></fieldset></dl></div>

      <small id='1leUl'></small><noframes id='1leUl'>

    2. 在 .NET 中,事件将在哪个线程中处理?

      In .NET, what thread will Events be handled in?(在 .NET 中,事件将在哪个线程中处理?)

          <i id='O4S8s'><tr id='O4S8s'><dt id='O4S8s'><q id='O4S8s'><span id='O4S8s'><b id='O4S8s'><form id='O4S8s'><ins id='O4S8s'></ins><ul id='O4S8s'></ul><sub id='O4S8s'></sub></form><legend id='O4S8s'></legend><bdo id='O4S8s'><pre id='O4S8s'><center id='O4S8s'></center></pre></bdo></b><th id='O4S8s'></th></span></q></dt></tr></i><div id='O4S8s'><tfoot id='O4S8s'></tfoot><dl id='O4S8s'><fieldset id='O4S8s'></fieldset></dl></div>
            <tbody id='O4S8s'></tbody>
            <legend id='O4S8s'><style id='O4S8s'><dir id='O4S8s'><q id='O4S8s'></q></dir></style></legend>
            • <bdo id='O4S8s'></bdo><ul id='O4S8s'></ul>
              • <small id='O4S8s'></small><noframes id='O4S8s'>

              • <tfoot id='O4S8s'></tfoot>
                本文介绍了在 .NET 中,事件将在哪个线程中处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我尝试在 C# 中实现生产者/消费者模式.我有一个监视共享队列的消费者线程和一个将项目放入共享队列的生产者线程.生产者线程被订阅以接收数据......也就是说,它有一个事件处理程序,并且只是坐在那里等待 OnData 事件触发(数据是从第 3 方 api 发送的).当它获取数据时,它会将其放在队列中,以便消费者可以处理它.

                I have attempted to implement a producer/consumer pattern in c#. I have a consumer thread that monitors a shared queue, and a producer thread that places items onto the shared queue. The producer thread is subscribed to receive data...that is, it has an event handler, and just sits around and waits for an OnData event to fire (the data is being sent from a 3rd party api). When it gets the data, it sticks it on the queue so the consumer can handle it.

                当 OnData 事件在生产者中触发时,我原以为它会由我的生产者线程处理.但这似乎不是正在发生的事情.OnData 事件似乎是在一个新线程上处理的!.net 总是这样工作吗?事件是在自己的线程上处理的吗?我可以控制引发事件时哪个线程将处理事件吗?如果几乎同时引发数百个事件会怎样……每个事件都有自己的线程吗?

                When the OnData event does fire in the producer, I had expected it to be handled by my producer thread. But that doesn't seem to be what is happening. The OnData event seems as if it's being handled on a new thread instead! Is this how .net always works...events are handled on their own thread? Can I control what thread will handle events when they're raised? What if hundreds of events are raised near-simultaneously...would each have its own thread?

                推荐答案

                重新阅读问题后,我想我现在明白了问题所在.你基本上得到了这样的东西:

                After re-reading the question, I think I understand the problem now. You've basically got something like this:

                class Producer
                {
                    public Producer(ExternalSource src)
                    {
                        src.OnData += externalSource_OnData;
                    }
                
                    private void externalSource_OnData(object sender, ExternalSourceDataEventArgs e)
                    {
                        // put e.Data onto the queue
                    }
                }
                

                然后你有一个消费者线程从队列中拉出东西.问题是 OnData 事件是由您的 ExternalSource 对象触发的 - 在它碰巧运行的任何线程上.

                And then you've got a consumer thread that pulls stuff off that queue. The problem is that the OnData event is fired by your ExternalSource object - on whatever thread it happens to be running on.

                C# event 基本上只是一个易于使用的委托集合,触发"事件只会导致运行时循环遍历所有委托并一次触发一个.

                C# events are basically just an easy-to-use collection of delegates and "firing" an event just causes the runtime to loop through all of the delegates and fire them one at a time.

                因此,您的 OnData 事件处理程序将在 ExternalSource 正在运行的任何线程上被调用.

                So your OnData event handler is getting called on whatever thread the ExternalSource is running on.

                这篇关于在 .NET 中,事件将在哪个线程中处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

              • <tfoot id='XidEj'></tfoot>
                  • <small id='XidEj'></small><noframes id='XidEj'>

                      <tbody id='XidEj'></tbody>

                        <i id='XidEj'><tr id='XidEj'><dt id='XidEj'><q id='XidEj'><span id='XidEj'><b id='XidEj'><form id='XidEj'><ins id='XidEj'></ins><ul id='XidEj'></ul><sub id='XidEj'></sub></form><legend id='XidEj'></legend><bdo id='XidEj'><pre id='XidEj'><center id='XidEj'></center></pre></bdo></b><th id='XidEj'></th></span></q></dt></tr></i><div id='XidEj'><tfoot id='XidEj'></tfoot><dl id='XidEj'><fieldset id='XidEj'></fieldset></dl></div>
                          <bdo id='XidEj'></bdo><ul id='XidEj'></ul>
                          <legend id='XidEj'><style id='XidEj'><dir id='XidEj'><q id='XidEj'></q></dir></style></legend>