问题在C#与RFID线程 [英] Question on C# threading with RFID

查看:222
本文介绍了问题在C#与RFID线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于确保我采取了正确的方法,正确处理线程的反馈。我有一种感觉,我可能需要设置一些我自己的线程,因此所有的反馈是值得欢迎的。

My question is for feedback on making sure I am taking the right approach, and handling the threading correctly. I have a feeling I may need to set of some of my own threads, so all feedback is welcome.

我的问题是,从零个或多个RFID读取RFID标签读者。我没有问题,所以从几个阅读将不再是一个问题,可以读取单个读者。由读取器读取标签的每个标签或批处理由一个.net事件交付。

The problem I have is reading RFID tags from zero or more RFID readers. I can read for a single reader without an issue so reading from several will not be an issue. Each Tag or batch of Tags read by the reader is delivered by a .Net Event.

我的计划是设置一个ReaderControl类,它保持了读者,连接起,停止等,该类会​​听的 TagRead 从读者的事件。在它处理的每个事件(每250毫秒粗略地),它把读取标签ID(字符串)到HashSet的,让他们唯一的,HashSet的位于ReaderControl。该ReaderControl将包含一个计时器,该火灾/每经过500毫秒,这TimerElapsed事件是由ReaderControl处理,这将打包到目前为止,所有的读者阅读标签和养 TagsRead 事件。这样做的目的是让事件触发到最低限度,减少重复的标签。

My plan is to setup a ReaderControl class, which maintains the readers, connection, starting, stopping, etc. This class will listen to the TagRead events from the readers. On each event it handles (roughly every 250ms) it puts the read tag ids (a string) into a HashSet to keep them unique, the HashSet is located in ReaderControl. The ReaderControl will contain a timer, that fires/elapses every 500ms, this TimerElapsed event is handled by the ReaderControl which will package up the tags read from all readers so far and raise a TagsRead event. The purpose of this is to keep event firing to a minimum and reduce duplicate tags.

TagsReads 事件被称为TagTranslator另一个类处理。通过标签ID(字符串),并制定出标签是指,即IPerson对象是什么这个类将循环。本课程将在翻译用的 PeopleSeen 事件。

The TagsReads event is handled by another class called TagTranslator. This class will loop through the tag ids (strings) and work out what the tag refers to, i.e. IPerson object. This class will fire an event on completion of the translation with a PeopleSeen event.

PeopleSeen 事件被处理通过在GUI(MVP模式)的模式。总的想法是一个GUI显示是显示其通过RFID阅读器的人的名字。显示的是简单,但很明显标签被读取asych和被翻译成要显示真实的对象罩下。

The PeopleSeen event is handled by a model in a GUI (MVP pattern). The overall idea is a GUI display is shows names of people which pass through the RFID readers. The display is simply but obviously under the hoods tags are being read in asych and been translated to "real" objects to be displayed.

你觉得ReaderControl应该运行自己的线程,我认为它应该。我如何去在自己的线程包装该类只保留读取标签,无论什么GUI在做什么。另外,你觉得当TagTranslator时处理事件应该创建线程来处理翻译。

Do you feel ReaderControl should be running on its own thread, I think it should. How do I go about packaging this class in its own thread to just keep reading tags regardless of what the GUI is doing. Also, do you think when the TagTranslator when handling events should create threads to handle the translation.

推荐答案

我会建议,而不是使用事件,您可以使用并发队列的数据结构和多生产,单次消费模式。想想标签阅读器线程作为生产者和处理线程作为消费者的喜爱。

I would suggest that, rather than using events, you use a concurrent queue data structure and a multiple producer, single consumer model. Think of the tag reader threads as producers, and the processing thread as consumers.

当一个线程从读取器接收到一个标签,它添加标签队列中,不担心重复或任何东西。哎呀,你可能的希望的重复的一些点的信息。没有理由把它扔出去就在这里。

When a thread receives a tag from a reader, it adds that tag to the queue, not worrying about duplicates or anything. Heck, you might want that duplicate information at some point. No reason to throw it out right here.

在队列中的消费者等待,取放物品起飞和处理它们一次。

The consumer waits on the queue, taking items off and processing them one at a time.

BlockingCollection 类是为这个完美的。

// Shared queue.  Assuming that a tag is simply a string.
BlockingCollection<string> TagQueue = new BlockingCollection<string>();

// Tag reader threads (producers)
while (!ShutdownMessageReceived)
{
    string tag = GetTagFromReader(); // however that's done
    TagQueue.Add(tag);
}

// Processing thread (consumer)
while (!ShutdownMessageReceived)
{
    string tag = TagQueue.Take();
    // process the tag
}



BlockingCollection 支持多个生产者和消费者,所以你可以有很多的每一个你喜欢的。在方法将阻塞,直到项目可用。这是一个非忙等待,所以没有投票的开销。

BlockingCollection supports multiple producers and consumers, so you can have as many of each as you like. The Take method will block until an item is available. It's a non-busy wait, so there's no polling overhead.

这种事情是很容易与 BlockingCollection ,并为执行得非常好干净和简单的代码。

This kind of thing is very easy to implement with BlockingCollection, and makes for clean and simple code that performs very well.

这篇关于问题在C#与RFID线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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