手持机如何连续按住扫描键扫描?

1. 定义线程变量和委托方法(供子线程反调主线程使用)

        private Thread scanThread;
        private delegate void invokeSetScanedData(string data);
        private delegate void invokeSetScanedData2(barCodeInfoBean bean);


2.  扫描键按下去方法:

     private void ClInTaskDetail_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 238)
            {
                //开始线程 连续扫描条码
                if (scanThread == null)
                {
                    scanThread = new Thread(new ThreadStart(ScanBarCode));
                    scanThread.IsBackground = true;
                    scanThread.Start();
                }
            }
        }


      private void ScanBarCode() {
          while (1>0)
           { 
            string strBarcode = string.Empty;
            if (CLR_Barcode_1D.Scan(out strBarcode))
            {
                CommonHelper.PlaySound();
                ScanedBarcodeType barcodeType = CommonHelper.GetScanedBarcodeType(strBarcode);
                switch (barcodeType)
                {
                    case ScanedBarcodeType.MaterialCode:
                        if (this.txtErpNo.InvokeRequired) {
                            Invoke(new invokeSetScanedData(SetMaterialScanedData), new Object[] { strBarcode.Trim() });                        
                        }
                        //this.txtErpNo.Text = strBarcode.Trim();
                        //this.txtErpNo.Focus();
                        break;
                    case ScanedBarcodeType.BoxCode:
                        if (this.txtBoxID.InvokeRequired)
                        {
                            Invoke(new invokeSetScanedData(SetBoxScanedData), new Object[] { strBarcode.Trim() });
                        }




                        //this.txtBoxID.Text = strBarcode.Trim();
                        //this.txtBoxID.Focus();
                        break;
                    case ScanedBarcodeType.BatchCode:
                        if (this.txtBatchID.InvokeRequired)
                        {
                            Invoke(new invokeSetScanedData(SetBatchScanedData), new Object[] { strBarcode.Trim() });
                        }
                        //this.txtBatchID.Text = strBarcode.Trim();
                        //this.txtBatchID.Focus();
                        //this.GetLeftCnt(txtErpNo.Text.Trim(), txtBatchID.Text.Trim()); //20160115 fix 带不出数量问题
                        break;
                    case ScanedBarcodeType.BarcodeBinding:


                        //barCodeInfoBean bean = new BarcodeBindingBLL().GetBarcodeBindingInfo(strBarcode.Trim());
                        //if (!string.IsNullOrEmpty(bean.erpno) && !string.IsNullOrEmpty(bean.batchId))
                        //{
                        //    //this.txtErpNo.Text = bean.erpno;
                        //    //this.txtBatchID.Text = bean.batchId;
                        //    //this.txtBoxID.Focus();
                        //    if (this.txtErpNo.InvokeRequired && this.txtBatchID.InvokeRequired) {
                        //        Invoke(new invokeSetScanedData2(SetBarcodeBindingScanedData), new Object[] { bean });                            
                        //    }
                        //}
                        //else
                        //{
                        //    this.scanThread.Abort();
                        //    this.scanThread = null;  
                        //    MessageBox.Show("您扫描的条码下面没有物料和批次信息,请检查!", "警告");                             
                        //}


                        if (this.txtErpNo.InvokeRequired && this.txtBatchID.InvokeRequired) {
                            Invoke(new invokeSetScanedData(SetBarcodeBindingScanedData2), new Object[] { strBarcode.Trim() });       
                        }






                        break;


                    default:
                        this.scanThread.Abort();
                        this.scanThread = null;  
                        MessageBox.Show("条码长度不符合条件,请检查!", "警告");                       
                        break;


                }
              }


              Thread.Sleep(300);


            }
        
        
        
        }



3. 扫描按键抬起,则中断连续扫描线程

    private void ClInTaskDetail_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 238)
            {
                //扫描黄键抬起,则结束连续扫描
                this.scanThread.Abort();
                this.scanThread = null;              
            }

}



4. 如何在连续扫描线程中调用主线程的方法:

    4.1 先用this.Control.InvokeRequired判断

    4.2 再用Invoke调用委托方法,记住WebSerive也必须写在委托方法里面去。

    

    private void SetMaterialScanedData(string data)
        {
            this.txtErpNo.Text = data;
            this.txtErpNo.Focus();
        }


        private void SetBatchScanedData(string data)
        {
            this.txtBatchID.Text = data;
            this.txtBatchID.Focus();
            this.GetLeftCnt(txtErpNo.Text.Trim(), txtBatchID.Text.Trim()); //20160115 fix 带不出数量问题
        }


        private void SetBoxScanedData(string data)
        {
            this.txtBoxID.Text = data;
            this.txtBoxID.Focus();
        }


        private void SetBarcodeBindingScanedData(barCodeInfoBean bean)
        {
            this.txtErpNo.Text = bean.erpno;
            this.txtBatchID.Text = bean.batchId;
            this.txtBoxID.Focus();
        }




        private void SetBarcodeBindingScanedData2(string data)
        {
            //注意:连续扫描开了新线程,如果该线程中还想调用WebService去获得条码绑定信息,就用把调用WebService的代码也放入Invoke中
            barCodeInfoBean bean = new BarcodeBindingBLL().GetBarcodeBindingInfo(data);
            if (!string.IsNullOrEmpty(bean.erpno) && !string.IsNullOrEmpty(bean.batchId))
            {
                this.txtErpNo.Text = bean.erpno;
                this.txtBatchID.Text = bean.batchId;
                this.txtBoxID.Focus();
            }
        }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值