zoukankan      html  css  js  c++  java
  • C#异步数据处理-WorkQueue

    介绍

    工作队列主要用于异步处理消息,详细介绍参考其他文章,这里主要提供使用方法

    类似方法有List、HashMap、Dir,但是性能略逊一筹。

    场景举例

    硅晶片标刻:

    通讯协议采用TCP协议

    1、程序(Server)对接上游LAMA机器(Client),接受标刻条码信息。

    2、程序(Client)控制激光打标机(Server),在硅晶片上标刻条码

    3、程序(Client)读取扫码器(Server)读码信息,并于标刻条码信息对比

    简单调用举例

    struct recivecode
        {
            public string ID;
            public string STATION;
        }
    WorkQueue<recivecode> l_queue = new WorkQueue<recivecode>();
    WorkQueue<long> l_markcode = new WorkQueue<long>();
    recivecode REC = new recivecode();
    REC.ID = IDimforation;
    REC.STATION = station;
    l_queue.EnqueueBox(REC);
    recivecode code = l_queue.TryDequeueBox();
    Console.WriteLine(code.ID);
    

    源代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.Concurrent;
    using System.Threading;
    
    namespace laser_code
    {
        /// <summary>
        /// 表示线程安全的队列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class WorkQueue<T> : ConcurrentQueue<T>
        {
            private int _size = 100;
            private DateTime _time;
            public DateTime Time
            {
                get { return _time; }
                set { _time = value; }
            }
            public int Size
            {
                get { return _size; }
                set { _size = value; }
            }
            /// <summary>
            /// 移除并返回开始处的对象
            /// </summary>
            /// <returns></returns>
            public T TryDequeueBox()
            {
                T _obj = default(T);
                if (!this.IsEmpty)
                {
                    this.TryDequeue(out _obj);
                    this._time = DateTime.Now;
                }
                return _obj;
            }
            /// <summary>
            /// 将对象添加到队列末尾
            /// </summary>
            /// <param name="box"></param>
            public void EnqueueBox(T box)
            {
                if (this._size > this.Count)
                {
                    this.Enqueue(box);
                    _time = DateTime.Now;
                }
                else
                {
                    this.Clear();
                }
            }
            /// <summary>
            /// 清空队列
            /// </summary>
            /// <returns></returns>
            public List<T> Clear()
            {
                T _obj = default(T);
                List<T> objectT = new List<T>();
                do
                {
                    this.TryDequeue(out _obj);
                    objectT.Add(_obj);
                } while (this.Count != 0);
                return objectT;
            }
        }
    }
    
  • 相关阅读:
    PythonのTkinter基本原理
    使用 Word (VBA) 分割长图到多页
    如何使用 Shebang Line (Python 虚拟环境)
    将常用的 VBScript 脚本放到任务栏 (Pin VBScript to Taskbar)
    关于 VBScript 中的 CreateObject
    Windows Scripting Host (WSH) 是什么?
    Component Object Model (COM) 是什么?
    IOS 打开中文 html 文件,显示乱码的问题
    科技发展时间线(Technology Timeline)
    列置换密码
  • 原文地址:https://www.cnblogs.com/aqyl/p/12389318.html
Copyright © 2011-2022 走看看