2015-07-22 18 views
5

Tôi có 3 lớp là Login, Barcode và Main.
Lớp đăng nhập chỉ chứa xác thực của người dùng.
lớp mã vạch có đoạn mã sau đoạn:Làm cách nào để tránh trùng lặp đăng ký sự kiện?

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

    } 

Trong thời gian ở lớp học chính, các subsciption của sự kiện mã vạch được thực hiện:

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 
     } 

    } 

Việc sao thuê bao sự kiện xảy ra khi tôi cố gắng đăng xuất và đăng nhập lại.
Khi tôi cố gỡ lỗi, BarcodeReadOut được gọi hai lần.
Khi đăng xuất, removeInstance() được gọi và biểu mẫu Chính là Close() và Dispose() trước khi mở màn hình đăng nhập.
Ai đó có thể giúp tôi về cách tôi có thể tránh trùng lặp các sự kiện đã nói không?

Tôi cũng đã làm điều này trước khi đăng ký sự kiện này nhưng không có gì xảy ra:

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

bạn có thể xóa tất cả eventsubscriptions với phản xạ. Hãy xem tại đây http://stackoverflow.com/questions/91778/how-to-remove-all-event-handlers-from-a-control –

+1

bạn có thể kiểm tra 'mã vạch.BarcodeReadOut == null' – Hassan

+1

Liên kết ở trên là tốt, nhưng hãy chắc chắn để đọc nó thông qua, như là câu trả lời chấp nhận dường như không phải là tốt nhất. – TaW

Trả lời

0

Bạn nên thêm và loại bỏ các xử lý như sau:

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 
    } 

} 

Ngoài ra: Bạn không cần phải xác định một đại biểu tùy chỉnh, bạn có thể sử dụng thông tin chung chung EventHandler:

public event EventHandler<BarcodeEventArgs> BarcodeReadOut; 
0

Sẽ là tốt để di chuyển tất cả logic của bạn hoạt động với Mã vạch thành một lớp riêng biệt. Và nó có thể là tốt để thêm một sự kiện tùy chỉnh thông báo cho các lớp khác (một lớp Mẫu trong trường hợp của bạn) sự kiện đã xảy ra:

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

và sử dụng nó như thế này:

BarcodeWorker barcode = new BarcodeWorker(); 

barcode.scanComplete += // your delegate with event handler or with anonymous method here; 
Các vấn đề liên quan