c# - WCF Completed Event is getting called multiple times -


i making asynchronous call wcf service methods , generated completed event on button click:

private void onsearchproductclick(object sender, routedeventargs e) {     service.getproductscompleted += new eventhandler<getproductscompletedeventargs>(webservice_getproductscompleted);     producttype producttype = (producttype)cboproducttype.selecteditem;     _producttypeid = producttype.producttypeid;     service.getproductsasync(txtname.text, txtcode.text, _producttypeid); } 

problem is, webservice_getproductscompleted event gets called multiple times. when click button first time gets called once, when click second time gets called twice when click third time gets called thrice , on. unusual behavior. why happening , how can resolve it?

here webservice_getproductscompleted event:

public void webservice_getproductscompleted(object sender, catalogueservicereference.getproductscompletedeventargs e) {     if (e.result.count != 0)     {         pagedcollectionview pagingcollection = new pagedcollectionview(e.result);         pgrproductgrids.source = pagingcollection;         grdproductgrid.itemssource = pagingcollection;         pgrproductgrids.visibility = visibility.visible;     } } 

the problem line:

service.getproductscompleted += new eventhandler<getproductscompletedeventargs>(webservice_getproductscompleted); 

you should call form load event, not here. because every time call these methods (onsearchproductclick) add same handler again gets executed multiple times.

other option un-register first , register again.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -