lambda - Event is not Called C# -
i'm making nfc library, need raise event, when card has been read.
in mainproject call library this:
reader = new nfcreader(); if (reader.initialize()) { reader.uidreceived += (s, args) => displaytext(string.format("uid received: {0}", args.uid)); }
my library:
public class nfcreader { private scardmonitor monitor; public event eventhandler<nfcreadereventargs> uidreceived; public bool initialize() { // when card inserted, event raised // want read uid of card monitor = new scardmonitor(new scardcontext(), scardscope.system); monitor.cardinserted += (s,a) => getuid(a.readername); return true; } private void getuid(string readername) { string uid = myuidgetter(readername); onuidreceived(uid); } private void onuidreceived(string uid) { var handler = uidreceived; if (handler == null) { handler(this, new nfcreadereventargs(uid)); } } }
the debugger steps through handler(this, new nfcreadereventargs(uid));
, displaytext
method doesn't called. ideas?
i guess typo check handler null? ;)
maybe initialize return false. , debugger steps through because won't call anything. made sure if.
i recommend use handler != null instead , make sure initialize returns true.
Comments
Post a Comment