如何避免重复事件订阅?

时间:2015-07-22 06:01:01

标签: c# events duplication subscribe

我有3个类,即Login,Barcode和Main。
登录类只包含用户的身份验证。
条形码类具有以下代码段:

    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()并且Main表单是Close()和Dispose()。
有人可以帮助我如何避免重复上述事件?

我在注册事件之前也做过这个,但没有任何反应:

    public Main()
    {
        barcode.BarcodeReadOut -= new barcode.BarcodeReadHandler(getBarcodeStr);
        barcode.BarcodeReadOut += new barcode.BarcodeReadHandler(getBarcodeStr);
    }

2 个答案:

答案 0 :(得分:0)

您应该按如下方式添加和删除处理程序:

public partial class Main : Form
{
  private Barcode barcode = null;

  public Main()
  {
    barcode.BarcodeReadOut += getBarcodeStr;
  }

  //This is called before log-out.
  public void removeInstance() 
  {
    barcode.BarcodeReadOut -= getBarcodeStr;
  }

  private void getBarcodeStr(object sender, BarcodeEventArgs e)
  {
    //some code
  }

}

另外:您不需要定义自定义委托,您可以使用通用EventHandler

public event EventHandler<BarcodeEventArgs> BarcodeReadOut;

答案 1 :(得分:0)

将与Barcode一起使用的所有逻辑移到单独的类中会很好。添加一个自定义事件可能会很好,该事件通知其他类(在您的情况下为Form类)发生了该事件:

echo round( 3.66666667 , 1); 

并像这样使用它:

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

}

class BarcodeWorker
{
    private Barcode barcode = null;
    private BarcodeReadHandler handler;
    public event BarcodeEventArgs scanComplete;

    BarcodeWorker(Barcode barcode) 
    {
       if(barcode == null) this.barcode = barcode;
    }

    public AddEventHandler()
    {
       if(handler != null) return;
       handler = new BarcodeReadHandler(getBarcodeStr);
       barcode.BarcodeReadOut += handler;
    }

    //This is called before log-out.
    public void RemoveEventHandler() 
    {
       barcode.BarcodeReadOut -= handler;
       handler = null;
    }

    private void getBarcodeStr(object sender, BarcodeEventArgs e)
    {
       scanComplete(sender, e);
    }
}