C # Delegate event self-understanding

Source: Internet
Author: User

It's hard for beginners to say that C # delegates and events, but it's easy to jump over it.

I don't know how long this is going to be, why it's so hard for some people to be simple, okay, this is a must.

Requirements: Card reader, I want to go to the program to call the card issuer method, get the cards card number information

Ordinary people understand: very simple, card one side, click on the reading of the event can be

I: Want to make it public DLL, use event registration to implement read card number

1. New Class library readertest--"reader

Public Reader () {}--> Constructor (It also writes the method of connecting the card reader)

2. Defining delegates

public delegate void Readereventhandler (Cardinfo card);

3. Defining events

public event Readereventhandler Readerevent;

4. Event triggering method

protected virtual void Readercard (Cardinfo card)

{//Prevent possible multi-threaded synchronization issues.

ReadercardhandlerTmpevent = readercardevent;      if (tmpevent! = null) {tmpevent (e); }
}5. Information class public class cardinfo{//a,b property slightly public cardinfo (string a,string b) {assignment slightly}}

6. External method Interface class invocation method implements event data transfer channel port

public void Getcardinfo ()

{

Logical Method Processing Branch

ULONG Cardno;            int cardtype; int nret = Card_readcardno(&cardno, &cardtype);                if (nret = = 0) {string Cardno = cardno.tostring ("x8"); String cardtype = Cardtype.tostring (); Cardinfo card = new Cardinfo (cardno,cardtype); //Pass event method Readcard(card);            } else {Console.WriteLine ("Cannot connect to a card"); }

}

7. Build DLL

8. Use of interface classes (add references)

Instantiate class first

Registering events in Load

Reader. readercardevent + = new XZX. Readersu.readercard. Readercardhandler (readcard);

Readcard method Implementation is consistent with internal delegate parameters

private void Readcard(XZX. ReaderSU.ReaderCard.CARDINFO e) {string cardno = E.cardno;           string cardtype = E.cardtype; Update interface element this.                Invoke (new Action () = {Lblcardno.text = Cardno;                Lbltype.text = Cardtype;            Lbltime.text = DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss");                   })); }

C # Delegate event self-understanding

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.