2015-07-22 6 views
5

मेरे पास 3 कक्षाएं हैं जैसे लॉगिन, बारकोड, और मुख्य।
लॉगिन कक्षा में केवल उपयोगकर्ताओं का प्रमाणीकरण शामिल है।इवेंट सदस्यता के डुप्लिकेशन से कैसे बचें?

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

    } 

मुख्य वर्ग में, बारकोड घटना के subsciption किया जाता है जबकि:

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 
     } 

    } 

घटना सदस्यता के दोहराव होता है जब मैं लॉग आउट करना और कोशिश
बारकोड वर्ग निम्नलिखित स्निपेट कोड है फिर से लॉगिन करें।
जब मैंने डीबग करने का प्रयास किया, तो बारकोडरडऑट को दो बार बुलाया जाता है।
लॉगआउट में, removeInstance() को कॉल किया जाता है और लॉगिन स्क्रीन खोलने से पहले मुख्य फॉर्म बंद() और निपटान() है।
क्या कोई मेरी मदद कर सकता है कि मैं उन घटनाओं के दोहराव से कैसे बच सकता हूं?

मैं भी घटना पंजीकरण से पहले यह किया है, लेकिन कुछ नहीं होता:

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

आप सभी को साफ़ कर सकते और यह कि (आपके मामले में एक प्रपत्र वर्ग) अन्य वर्गों सूचित करता है कि घटना हुई है एक कस्टम घटना जोड़ने के लिए अच्छा हो सकता है प्रतिबिंब के साथ घटनाओं सदस्यता। यहां एक नज़र डालें http://stackoverflow.com/questions/91778/how-to-remove-all-event-handlers-from-a-control –

+1

आप 'बारकोड' के लिए जांच सकते हैं। बारकोड रीडऑट == नल' – Hassan

+1

उपरोक्त लिंक अच्छा है, लेकिन इसे पढ़ने के लिए सुनिश्चित करें, क्योंकि स्वीकृत उत्तर सबसे अच्छा नहीं लगता है। – TaW

उत्तर

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; 
0

यह अच्छा होगा अपने सभी तर्कों को स्थानांतरित करने के लिए जो एक अलग वर्ग में बारकोड के साथ काम करता है।

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

और इस तरह इसका इस्तेमाल:

BarcodeWorker barcode = new BarcodeWorker(); 

barcode.scanComplete += // your delegate with event handler or with anonymous method here; 
संबंधित मुद्दे