C#是否维护事件队列?

huangapple go评论65阅读模式
英文:

Does C# maintain queue for events?

问题

我正在开发一个物联网(IoT)应用程序,并使用C#事件来订阅MQTT消息。我正在使用一个库,当接收到消息时会触发一个事件。我已经编写了一个用于处理消息的异步事件处理程序。

消息接收的速度高于处理它们的速度。在这种情况下,我发现消息处理存在延迟,尽管事件处理程序是异步的。

我的问题是,C#是否在内部维护某个事件处理程序的队列?如果是的话,我能否访问该队列以进行诊断目的?通过访问该队列,我可以分析导致延迟的确切负载。

编辑

以下是我的设置:

  1. 我有一个.NET Framework 4.6.2控制台应用程序。
  2. 在应用程序中,我使用MQTTnet库来订阅MQTT消息。
  3. 以下是我用于订阅的代码,
var mqttFactory = new MqttFactory();

var client = mqttFactory.CreateMqttClient();

client.ApplicationMessageReceivedAsync += async e =>
{
    // 处理消息。例如:更新数据库
}

var clientOptions = mqttFactory.CreateClientOptionsBuilder()
    .WithTcpServer("mqtt-broker-host", 9000)
    .Build();

await client.ConnectAsync(clientOptions);
await client.SubscribeAsync("mqtt-topic");
  1. 在处理程序中,我处理消息。处理的主要部分是更新数据库中的记录。
英文:

I am working on an IoT application and using C# events to subscribe to MQTT messages. I am using a library that raises an event when a message is received. I have written an Asynchronous EventHandler for processing the message.

The rate at which the messages are being received is higher than the rate of processing them. In this case, I see a delay in the processing of messages, even though the EventHandler is asynchronous.

My question is does C# internally maintain some queue for EventHandlers? If, so can I access that queue for diagnostics purposes? By accessing that queue, I can analyze the exact load which causes the delay.

EDIT

Following is my setup,

  1. I have .NET Framework 4.6.2 console application.
  2. In the application I use MQTTnet library for subscribing to MQTT messages.
  3. Following is the code I use for subscribing,
var mqttFactory = new MqttFactory();

var client = mqttFactory.CreateMqttClient();

client.ApplicationMessageReceivedAsync += async e =>
{
    // Process the message. Ex: Update database
}

var clientOptions = mqttFactory.CreateClientOptionsBuilder()
    .WithTcpServer("mqtt-broker-host", 9000)
    .Build();

await client.ConnectAsync(clientOptions);
await client.SubscribeAsync("mqtt-topic");
  1. In the handler I process the message. The main part of processing is updating the records in the database.

答案1

得分: 3

EventHandler 旨在是同步的;没有任何类型的队列。如果您需要某种排队机制,也许考虑使用 Channel<T>,它是一个异步队列样的设备,用于独立的写入("pub")和读取("sub");或者如果您的要求更复杂,可以使用一系列的进程内和进程外的消息队列/事件代理工具。

英文:

EventHandler is intended to be synchronous; there is no queue of any kind. If you want some kind of queuing mechanism, maybe consider Channel<T>, which is an async queue-like device intended for independent write ("pub") and read ("sub"); or if your requirements are more complex, a range of in-process and out-of-process message-queue / event broker tools are available.

答案2

得分: 1

没有,事件处理程序本质上只是委托列表。触发事件只是循环遍历所有委托,依次调用它们。

async 添加到方法中并不会自动使任何东西变成异步。它只允许您等待异步方法,但有时方法可能会谎称自己是异步的。

在C#中有一些队列,比如UI线程的消息队列,但我不认为这在这种情况下相关。

这听起来像是您想要使用性能分析器。这将告诉您代码的每个部分花费了多少CPU时间,以及如果某些代码由于某种原因被阻塞。最好的解决方案是通过优化或避免执行任何“慢”的操作来解决性能问题。

英文:

> My question is does C# internally maintain some queue for EventHandlers?

No, an event handler is essentially just a list of delegates. And raising an event just loops over all the delegates and calls them in turn.

Adding async to a method does not automatically make anything asynchronously. It just allow you to await asynchronous methods, but sometimes methods lie about being asynchronous.

There are some queues in C#, like the message queue for the UI thread, but I do not think this is relevant in this particular.

> If, so can I access that queue for diagnostics purposes. By accessing that queue, I can analyze the exact load which causes the delay.

This sounds like you want to use a performance profiler. This should tell you how much CPU time is spent in each portion of the code, and if some code is blocking for some reason. The best solution would be if you could solve your performance problem by optimization or avoid doing anything "slow".

huangapple
  • 本文由 发表于 2023年6月27日 17:46:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定