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();
}
}