C#中使用多线程的伪异步读取 [英] Pseudo asynchronous reading using multi threading in C#

查看:97
本文介绍了C#中使用多线程的伪异步读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#开发winforms应用程序。此应用程序使用ThingMagic USB Plus RFIDM5e阅读器扫描找到的标签。我有阅读器的手册,它是Mercury API,它提供了codelet示例。在附带的示例中,没有任何伪异步示例,但我理解这个概念,它需要多线程并且我没有线程经验。



我现在拥有的界面摘要,它有Gridview,它将使用Synchronous可接近的哪个结果读取一次显示扫描的标签,我需要它伪异步方法。所以,我想在单独的线程中完成读取操作。



这是手册中的捕获。



I am developing winforms application using C#. This app uses ThingMagic USB Plus RFID "M5e" reader in order to scan the tags that found. I have the manual of the reader which is Mercury API and it provides codelet sample. In the attached samples, there is not any Pseudo Asynchronous example, but I understand the concept, it needs multi-threading and I do not have experience in threading.

Summary of the interface that I have now, it has Gridview that will display the scanned tags using Synchronous approachable "Which result in read once", I need it to be Pseudo Asynchronous approach. So, Just I want to make the reading operation done in separate thread.

This is the capture from the manual.

Pseudo-Asynchronous Reading
In pseudo-asynchronous reading a synchronous search is looped over and over again
running indefinitely in a separate thread. Tags are off-loaded once every synchronous
search is completed. i.e., read listeners will be called once for every "/reader/read/
asyncOnTime" milliseconds. On all readers except the M6 and M6e pseudoasynchronous
reading is the only implementation used for background reading
operations. 











另见这个链接,这是类似的问题,但我无法实现。



c# - 使用连续RFID读取的开始/结束调用 - 堆栈溢出 [ ^ ]



我的尝试:








Also see this link, it is similar issue but i did not able to implement that.

c# - Using begin/end invoke with continuous RFID read - Stack Overflow[^]

What I have tried:

<pre>public Reader objReader = null;  // Create reader object that reads the scanned tags.
        private ReadTags[] arr1 = new ReadTags[0];
        private ReadTags[] arr = null;

        public MyCart()
        {
            InitializeComponent();
            //UpdateComport();
            dataGrid1.DataSource = arr1;
            generatedatagrid(); 

        }


     public void generatedatagrid()
        {
            DataGridTableStyle tableStyle = new DataGridTableStyle(); 
            tableStyle.MappingName = arr1.GetType().Name; // record instance 
            

            DataGridTextBoxColumn tbcName = new DataGridTextBoxColumn(); 
            tbcName.Width = dataGrid1.Width * 50 / 100;
            tbcName.MappingName = "ProName"; 
            tbcName.HeaderText = "Product Name";
            tableStyle.GridColumnStyles.Add(tbcName);

            tbcName = new DataGridTextBoxColumn();
            tbcName.Width = dataGrid1.Width * 25 / 100;
            tbcName.MappingName = "ProPrice";
            tbcName.HeaderText = "Price";
            tableStyle.GridColumnStyles.Add(tbcName);

            tbcName = new DataGridTextBoxColumn();
            tbcName.Width = dataGrid1.Width * 25 / 100;
            tbcName.MappingName = "ReadCount";
            tbcName.HeaderText = "Quantity";
            tableStyle.GridColumnStyles.Add(tbcName);

            //tbcName = new DataGridTextBoxColumn();
            //tbcName.Width = dataGrid1.Width * 12 / 100;
            //tbcName.MappingName = "";
            //tbcName.HeaderText = "Total";
            //tableStyle.GridColumnStyles.Add(tbcName);

            dataGrid1.TableStyles.Clear();
            dataGrid1.TableStyles.Add(tableStyle); // bind
        }

<pre>     private void btnReadOnce_Click(object sender, EventArgs e)
        {
            try
            {
                //int TotalTagCount = 0;
                btnReadOnce.Enabled = false;

              
               TagReadData[] tagID = objReader.Read(int.Parse(tbxReadTimeout.Text));
                    arr = new ReadTags[tagID.Length];
                    for (int i = 0; i < tagID.Length; i++)
                    {
                        arr[i] = new ReadTags(tagID[i]);
                        //TotalTagCount = TotalTagCount + tagID[i].ReadCount;
                    }
                    dataGrid1.DataSource = arr;
                    generatedatagrid();
                    //lblTotalTagCount.Text = TotalTagCount.ToString();
                    //lblUniqueTagCount.Text = tagID.Length.ToString();
                    btnReadOnce.Enabled = true;
                    btnReadOnce.Focus();
             

            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message, "Error");
                objReader.Destroy();
                objReader = null;
                btnReadOnce.Enabled = false;
                btnConnect.Text = "Connect";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Reader Message");
                btnReadOnce.Enabled = true;
            }
        }



        private void btnConnect_Click(object sender, EventArgs e)
        {

            try
            {
                if (btnConnect.Text.Equals("Connect"))
                {
                    String model = string.Empty;
                    string readeruri = "COM3";
                        //comboBox1.SelectedItem.ToString(); 
                    objReader = Reader.Create(string.Concat("eapi:///", readeruri));
                    objReader.Connect();
                    model = (string)objReader.ParamGet("/reader/version/model");
                    Reader.Region regionToSet = (Reader.Region)objReader.ParamGet("/reader/region/id");
                    if (objReader is SerialReader)
                    {
                        //if (regionToSet == Reader.Region.UNSPEC)
                        //{
                        //    if (model.Equals("M6e PRC"))
                        //    {
                        //        regionToSet = Reader.Region.PRC;
                        //    }
                        //    else
                        //    {
                        //        regionToSet = Reader.Region.NA;
                        //    }
                        regionToSet = Reader.Region.OPEN;
                    }
                    objReader.ParamSet("/reader/region/id", regionToSet);

                    if (model.Equals("M5e"))
                    {
                        SimpleReadPlan plan = new SimpleReadPlan(new int[] { 1 }, TagProtocol.GEN2);
                        objReader.ParamSet("/reader/read/plan", plan);
                    }
                    //if (model.Equals("M6e Nano"))
                    //{
                    //    SimpleReadPlan plan = new SimpleReadPlan(new int[] { 1 }, TagProtocol.GEN2);
                    //    objReader.ParamSet("/reader/read/plan", plan);
                    //}
                    btnConnect.Text = "Disconnect";
                    btnReadOnce.Enabled = true;
                    comboBox1.Enabled = false;
                    btnRefresh.Enabled = false;
                    lblReadTimeout.Enabled = true;
                    tbxReadTimeout.Enabled = true;
                    UpdateGrid();
                }
                else
                {
                    objReader.Destroy();
                    objReader = null;
                    btnReadOnce.Enabled = false;
                    comboBox1.Enabled = true;
                    lblReadTimeout.Enabled = false;
                    tbxReadTimeout.Enabled = false;
                    btnConnect.Text = "Connect";
                    btnRefresh.Enabled = true;
                    lblTotalTagCount.Text = "0";
                    lblUniqueTagCount.Text = "0";
                }
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message, "Error");
                objReader.Destroy();
                objReader = null;
                btnReadOnce.Enabled = false;
                btnConnect.Text = "Connect";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Reader message");
            }

        }


    public class ReadTags
    {
        private string epc;     // hexa ID
        //private string timestamp;
        private string readcount;   // Quantity 
        //public string rssi;
        private string proName;   // pro name
        private string proPrice;  // pro proice
        private int idDec;    // Converted id 

        public ReadTags()
        {
        }

        public ReadTags(TagReadData adddata)
        {
            epc = adddata.EpcString;
            idDec = int.Parse(epc, System.Globalization.NumberStyles.HexNumber);
            //timestamp = adddata.Time.ToString();
            readcount = adddata.ReadCount.ToString();
            //rssi = adddata.Rssi.ToString();
}

推荐答案

我认为您首先需要阅读任务(这是现代的线程方式)。

这是一篇很好的文章:任务并行库:n中的1个 [ ^ ]

正如您已经提到的Pseudo,最好不要让所有任务同时访问同一个USB端口。 br />
使用任务时,您可以例如使用 .ContinueWith 在任务完成时启动另一个任务。



哦,我也建议使用您的DataGrid的 DataSource BindingList ,这是一篇很好的文章:> A-Detailed-Data-Binding-Tutorial

你的任务可以然后继续在后台运行(长时间运行的任务)并更新 DataSource ,它会自动更新DataGrid。
I think you first need to read up on Tasks (which is the modern way of threads).
Here is a good article about it: Task Parallel Library: 1 of n[^]
As you already mentioned "Pseudo", it is probably best to not let all Tasks access the same USB port simultaneously.
When working with Tasks you can e.g. use .ContinueWith to start another Task when a Task is finished.

Oh, and I would also recommend using a DataSource or BindingList for your DataGrid, here is an excellent article about it: A-Detailed-Data-Binding-Tutorial
Your task could then keep running on the background (long running Task) and update the DataSource which automagically updates the DataGrid.


这篇关于C#中使用多线程的伪异步读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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