C# 控制 WndProc()接收处理消息的频率

JohnZhouQ 2014-03-15 04:45:49
我用终端机扫描信息后 打印扫描的信息 但是由于机器太灵敏 他会出现一秒内扫描很多次 导致会打印出多条 一样的信息
我现在想10秒内 只能处理一次 该怎么弄?。
代码如下:
DateTime? lastScanTime = null;
protected override void WndProc(ref Message msg)
{
try
{

if (lastScanTime == null)
{
lastScanTime = DateTime.Now;
}
else
{
TimeSpan span = DateTime.Now - lastScanTime.Value;
if (span.Seconds < 10)
return;
}

if (msg.Msg == WM_SCANCODE_ENGINE_NOTIFY) // BarCode的自定义消息通知
{

uint nDataLen = 0; //= (int)msg.WParam;
uint nBarCodeType = 0;// = (uint)msg.LParam;
byte[] BarCodeData;
barcode.GetBarCodeReaderRes(ref nBarCodeType, ref nDataLen, null);
if (nDataLen > 0)
{

string mess = "正在处理";
BarCodeData = new byte[nDataLen];
barcode.GetBarCodeReaderRes(ref nBarCodeType, ref nDataLen, BarCodeData);
string str1 = Encoding.Default.GetString(BarCodeData, 0, (int)nDataLen);
this._BaseForm.DecodeText(ref mess);
this._BaseForm.GetTick(str1, 1);
barcode.DoBeep(1, 50, 80);

}
if (_BaseForm.PrintMessage != "" && _BaseForm.isScan)
{
int ct = barcode.DoPrint(_BaseForm.PrintMessage, "宋体", 30, 100);

}
}
else
{
base.WndProc(ref msg);

}


}
catch (Exception ex)
{

}

}
...全文
194 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
JohnZhouQ 2014-03-15
  • 打赏
  • 举报
回复
谢了可以用了
  • 打赏
  • 举报
回复
if (!lastScanTime.HasValue || DateTime.Now.Subtract(lastScanTime.Value) > TimeSpan.FromSeconds(10)) { }
本拉灯 2014-03-15
  • 打赏
  • 举报
回复


 DateTime nextScanTime = DateTime.MinValue;

        protected override void WndProc(ref Message msg)
        {
            try
            {

                if (msg.Msg == WM_SCANCODE_ENGINE_NOTIFY && DateTime.Now>nextScanTime)   // BarCode的自定义消息通知
                {
                    nextScanTime = DateTime.Now.AddSeconds(10); //<===这增加10秒
                    uint nDataLen = 0; //= (int)msg.WParam;
                    uint nBarCodeType = 0;// = (uint)msg.LParam;
                    byte[] BarCodeData;
                    barcode.GetBarCodeReaderRes(ref nBarCodeType, ref  nDataLen, null);
                    if (nDataLen > 0)
                    {

                        string mess = "正在处理";
                        BarCodeData = new byte[nDataLen];
                        barcode.GetBarCodeReaderRes(ref nBarCodeType, ref  nDataLen, BarCodeData);
                        string str1 = Encoding.Default.GetString(BarCodeData, 0, (int)nDataLen);
                        this._BaseForm.DecodeText(ref mess);
                        this._BaseForm.GetTick(str1, 1);
                        barcode.DoBeep(1, 50, 80);

                    }
                    if (_BaseForm.PrintMessage != "" && _BaseForm.isScan)
                    {
                        int ct = barcode.DoPrint(_BaseForm.PrintMessage, "宋体", 30, 100);

                    }
                }
                else
                {
                    base.WndProc(ref msg);

                }


            }
            catch (Exception ex)
            {

            }

        }






110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧