【发布时间】:2023-03-23 16:09:02
【问题描述】:
我有 3 个类,即 Login、Barcode 和 Main。
登录类只包含用户的身份验证。
Barcode 类有如下 sn-p 码:
class Barcode
{
public delegate void BarcodeReadHandler(object sender, BarcodeEventArgs e);
public event BarcodeReadHandler BarcodeReadOut;
public Barcode()
{
//.. some codes for getting data on the scanner
BarcodeEventArgs args = new BarcodeEventArgs(scannedData);
BarcodeReadOut(this, args);
}
}
在 Main 类中,Barcode 事件的订阅完成:
public partial class Main : Form
{
private Barcode barcode = null;
public Main()
{
barcode.BarcodeReadOut += new barcode.BarcodeReadHandler(getBarcodeStr);
}
//This is called before log-out.
public void removeInstance()
{
barcode.BarcodeReadOut -= new barcode.BarcodeReadHandler(getBarcodeStr);
}
private void getBarcodeStr(object sender, BarcodeEventArgs e)
{
//some code
}
}
当我尝试注销并再次登录时,会发生重复订阅事件。
当我尝试调试时,BarcodeReadOut 被调用了两次。
在注销时,会调用 removeInstance(),主窗体是 Close() 和 Dispose(),然后再打开登录屏幕。
有人可以帮助我如何避免上述事件的重复吗?
我在注册活动之前也这样做了,但没有任何反应:
public Main()
{
barcode.BarcodeReadOut -= new barcode.BarcodeReadHandler(getBarcodeStr);
barcode.BarcodeReadOut += new barcode.BarcodeReadHandler(getBarcodeStr);
}
【问题讨论】:
-
您可以通过反射清除所有事件订阅。看这里stackoverflow.com/questions/91778/…
-
你可以检查
barcode.BarcodeReadOut == null -
上面的链接很好,但请务必仔细阅读,因为接受的答案似乎不是最好的。
-
在您的最后一段代码中,您删除另一个 BarcodeReadHandler 而不是添加。 (新)var bcr = 新条码.BarcodeReadHandler(getBarcodeStr);条形码.BarcodeReadOut -= bcr;条形码.BarcodeReadOut += bcr;会是正确的。
标签: c# events duplication subscribe