【发布时间】:2015-02-20 00:57:14
【问题描述】:
我从 FrmDelivery 调用“查找”表单。 Find 表单在尝试将条形码扫描到编辑框中时崩溃(相同的代码在应用程序的其他地方也可以正常工作,例如在 FrmDelivery 中)。
日志文件显示 FrmDelivery 是遇到异常的表单:
Message: Reached frmDelivery.StartRead
Date: 2/19/2015 7:39:39 PM
Message: From FrmDelivery.StartRead(): The scanner not enabled, Call Enable() first.; Inner Ex: ; Stack Trace: at
Symbol.Barcode.Actions.Read(ReaderData rd)
然而,在打开 Find 表单之前,我关闭了 FrmDelivery(下面的“this”):
private void buttonFind_Click(object sender, EventArgs e)
{
ExceptionLoggingService.Instance.WriteLog("Reached frmDelivery.buttonFind_Click");
this.Close(); // Close the Delivery form; this still leaves frmMain up
const HHSConsts.RecordTypes rt = HHSConsts.RecordTypes.Delivery;
frmFind ff = new frmFind(rt, dsdName);
ff.ShowDialog();
}
StartRead() 是抛出异常的方法:
private void StartRead()
{
ExceptionLoggingService.Instance.WriteLog("Reached
frmDelivery.StartRead");
try
{
// If we have both a reader and a reader data
if ((this.barcodeReader != null) && (this.barcodeReaderData !=
null))
{
if (this.barcodeReaderData.IsPending) return;
// Submit a read
this.barcodeReader.ReadNotify += this.barcodeEventHandler;
this.barcodeReader.Actions.Read(this.barcodeReaderData);
}
}
catch (Exception ex)
{
String msgInnerExAndStackTrace = String.Format(
"{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message,
ex.InnerException, ex.StackTrace);
ExceptionLoggingService.Instance.WriteLog(String.Format("From
FrmDelivery.StartRead(): {0}", msgInnerExAndStackTrace));
}
}
// StartRead() 在 FrmDelivery 的四个地方被调用:
private void textBoxUPC_PLU_GotFocus(object sender, EventArgs e)
{
textBoxUPC_PLU.BackColor = HHSConsts.BARSCAN_COLOR; // Why is this
not sticking?
ExceptionLoggingService.Instance.WriteLog("Reached
frmDelivery.textBoxUPC_PLU_GotFocus");
if (this.InitReader())
{
this.StartRead();
}
}
private void BarcodeReader_ReadNotify(object sender, EventArgs e)
{
ExceptionLoggingService.Instance.WriteLog("Reached
frmDelivery.BarcodeReader_ReadNotify");
try
{
Symbol.Barcode.ReaderData TheReaderData =
this.barcodeReader.GetNextReaderData();
if (TheReaderData.Result == Symbol.Results.SUCCESS)
{
// Handle the data from this read
this.HandleData(TheReaderData);
// Start the next read
this.StartRead();
}
}
catch (Exception ex)
{
String msgInnerExAndStackTrace = String.Format(
"{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message,
ex.InnerException, ex.StackTrace);
ExceptionLoggingService.Instance.WriteLog(String.Format("From
FrmDelivery.BarcodeReader_ReadNotify(): {0}",
msgInnerExAndStackTrace));
}
}
private void ReaderForm_Activated(object sender, EventArgs e)
{
ExceptionLoggingService.Instance.WriteLog("Reached
frmDelivery.ReaderForm_Activated");
// If there are no reads pending on barcodeReader, start a new read
if (!this.barcodeReaderData.IsPending)
{
this.StartRead();
}
}
private void textBoxId_GotFocus(object sender, EventArgs e)
{
ExceptionLoggingService.Instance.WriteLog("Reached
frmDelivery.textBoxId_GotFocus");
if (this.InitReader())
{
this.StartRead();
}
}
但是在表单关闭后,任何方法如何调用 StartRead()?
【问题讨论】:
-
我很困惑:你说Find表单崩溃了,但是日志摘录似乎说frmDelivery是源,也说是第一个代码块中关闭的表单。无论如何,frmDelivery 也是一个对话框吗?
-
是的,这就是我的问题 - 为什么关闭表单的事件会触发? FrmDelivery 通过 ShowDialog() 从 frmMain 调用;使用 ShowDialog() 从 FrmDelivery 调用 frmFind。
-
旁注:
this.barcodeReader.ReadNotify += this.barcodeEventHandler;看起来很麻烦,因为它似乎会多次添加此事件处理程序。这可能会使该方法运行多次。 -
@LarsTech:那么将代码移到构造函数中会更好,还是在连接处理程序之前先解开处理程序(-=)?
标签: c# forms barcode barcode-scanner scanning