c# check if event is null -



c# check if event is null -

i have event in webservice:

public event findproductsbycharacteristicscompletedeventhandler findproductsbycharacteristicscompleted { [methodimpl(methodimploptions.synchronized)] add together { _findproductsbycharacteristicscompleted += value; } [methodimpl(methodimploptions.synchronized)] remove { _findproductsbycharacteristicscompleted -= value; } }

and im checking if event value null later in class:

private void onfindproductsbycharacteristicsoperationcompleted(object arg) { var handler = _findproductsbycharacteristicscompleted; if (handler == null) return; handler(this, new findproductsbycharacteristicscompletedeventargs(completedeventargs.results, completedeventargs.error, completedeventargs.cancelled, completedeventargs.userstate)); }

your event implementation looks endless recursion. using property in implementation. alter this:

private findproductsbycharacteristicscompletedeventhandler _findproductsbycharacteristicscompleted; public event findproductsbycharacteristicscompletedeventhandler findproductsbycharacteristicscompleted { [methodimpl(methodimploptions.synchronized)] add together { _findproductsbycharacteristicscompleted += value; } [methodimpl(methodimploptions.synchronized)] remove { _findproductsbycharacteristicscompleted -= value; } }

and now, implement method this:

var handler = _findproductsbycharacteristicscompleted; if(handler == null) return; handler(this, new findproductsbycharacteristicscompletedeventargs(...));

this has advantage thread-safe.

even if detached lastly handler event after checked null before raised event, not exception, because operating on unchanged local variable.

c# events null

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -