1. Define thread variables and delegate methods (for child thread sung main thread use)
Private Thread Scanthread;
Private delegate void Invokesetscaneddata (string data);
Private delegate void InvokeSetScanedData2 (Barcodeinfobean bean);
2. Press the scan key down method:
private void Clintaskdetail_keydown (object sender, KeyEventArgs e)
{
if (E.keyvalue = = 238)
{
Start thread Continuous Scan barcode
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 does not have a quantity problem
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 ("There is no material and batch information under the barcode you are scanning, please check!") "," warning ");
//}
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 ("Barcode length does not meet the criteria, please check!") "," warning ");
Break
}
}
Thread.Sleep (300);
}
}
3. When the scan button is lifted, the continuous scan thread is interrupted
private void Clintaskdetail_keyup (object sender, KeyEventArgs e)
{
if (E.keyvalue = = 238)
{
Scan Huang lift, end continuous scan
This.scanThread.Abort ();
This.scanthread = null;
}
}
4. How to invoke the main thread in a continuous scan thread:
4.1 Use this first. Control.invokerequired judgment
4.2 Call the delegate method again with invoke, and remember that webserive must also be written in the delegate method.
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 does not have a quantity problem
}
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)
{
Note: The continuous scan opens a new thread, and if the thread also wants to call WebService to get the barcode binding information, use the code that calls webservice into the 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 ();
}
}
How does the handheld hold the scan key continuously?