C# 关于排队的建议

发布于 2024-10-25 13:14:14 字数 521 浏览 1 评论 0 原文

我正在就分配给我的一个项目寻求建议,我希望了解它是如何“专业”完成的,或者任何可以引导我走向正确方向的建议。

我有一个服务器部件,它接受来自客户端的命令并将字节流推送到串行端口。尽管多个客户端可以向该服务器发送命令,但我们的硬件一次只能处理一个命令。我的问题是在软件端排队。

我实现了一个 Queue 帮助程序类,该类还将数据插入到 DataSet 中,其中包含:请求客户端编号、消息数据(写入串行端口的字节数组)和消息类型(命令描述) 。它还将在 DataGrid(表单上)中列出队列命令。可能不是可行的方法,但就保留请求客户端和数据并以视觉方式显示队列而言,这是我能想到的唯一方法。

我在哪里处理队列的处理?我考虑过在自定义事件中处理它,如果 DataGrid 列表发生更改(添加/删除项目),则获取 DataSet 中的第一行数据并将其发送到串行端口。

如有任何意见或建议,我们将不胜感激。

谢谢。

编辑:我忘记补充一点,它确实需要来自串行端口的响应,以便将当前执行的命令从队列中删除。

I am seeking advice on a project I have been assigned and I'm looking to see how it's done "professionally," or any suggestions that can lead me to the right direction.

I have a server piece that accepts commands from clients and pushes out byte streams to a serial port. Although multiple clients can send commands to this server piece, our hardware only can handle one command at a time. My problem is with queueing in the software end.

I've implemented a Queue<T> helper class that also inserts data into a DataSet containing: the requesting client number, message data (byte array to write to serial port) and message type (command description). It will also list the queue commands in a DataGrid (on the Form). Probably not the way to go, but that's the only thing I can think of as far as retaining the requesting client and the data and showing, visually, the queue.

Where do I handle the processing of the queue? I thought about handling it on a custom event where if the DataGrid list changed (item added/removed), grab the first row of data in the DataSet and send it out to the serial port.

Any comments or suggestions are greatly appreciated.

Thanks.

Edit: I forgot to add that it does require a response from the SerialPort as well, in order for the current executed command to be removed from the queue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷血 2024-11-01 13:14:14

我将使用数据库表来存储命令队列。 Web 应用程序会将记录添加到队列并显示队列,然后单独的进程(例如 Windows 服务或控制台应用程序)将从数据库请求下一个命令并将其发送到串行端口。

I would use a database table to store the queue of commands. The web app would add records to the queue and display the queue, then a separate process (such as a Windows service or console app) would request the next command from the database and send it to the serial port.

伴我老 2024-11-01 13:14:14

客户端请求可以随时传入,它们可能会由某个代理类(WCF?)在其自己的线程/任务上处理。然后,该线程/任务需要与实际处理请求的模型“内部”的任务进行协调。

BlockingCollection 是一个很好的类。

服务器线程将阻塞,直到集合中有东西可以处理。然后它可以以线程安全的方式从集合中取出它并进行处理。这样做可以确保请求到达时可以被接受,但它们会被一次性处理。

这里要考虑的总体模式是生产者-消费者。

国杰

Client requests can come in at any time, they'll probably be handled by some proxy class (WCF?) on its own thread/task. Then that thread/ task needs to coordinate with the task that's 'inside' the model actually processing the requests.

A good class to do this with is the BlockingCollection.

The server-thread will block until there's something in the collection to work on. It can then take it from the collection in a thread safe manner and process it. Doing it this way ensures that the requests can be accepted when they arrive, but they are processed on at a time.

The overall pattern to think of here is producer-consumer.

GJ

墨小沫ゞ 2024-11-01 13:14:14

如果它是高交易量 Web 应用程序,您可能需要查看排队系统,例如 MSMQService Broker 队列RabbitMQ。然后,窗口服务可以拾取排队的项目并将其发送到串行端口。

If it is a high trasaction web application you might want to look at queueing system such as MSMQ, Service Broker Queue or RabbitMQ. Window service can then pick up the queued items and send it to serial port.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文