1

I am making an asynchronous call to wcf service methods and generated a 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, the webService_GetProductsCompleted event gets called multiple times. When click the Button for first time it gets called once, when I click second time gets called twice when click third time gets called thrice and so on. This is a very unusual behavior. Why is it happening and how can I resolve it?

Here is the 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;
    }
}
Huma Ali
  • 1,759
  • 7
  • 40
  • 66

2 Answers2

2

The problem is this line:

service.GetProductsCompleted += new EventHandler<GetProductsCompletedEventArgs>(webService_GetProductsCompleted);

You should call it from the form Load event, not here. Because every time you call these methods (OnSearchProductClick) you add the same handler again so it gets executed multiple times.

Other option is un-register it first and then register it again.

prashant
  • 2,181
  • 2
  • 22
  • 37
0

problem is here.

    private void OnSearchProductClick(object sender, RoutedEventArgs e)
    {
        service.GetProductsCompleted += new EventHandler<GetProductsCompletedEventArgs>(webService_GetProductsCompleted);
    }

By doing this, you are subscribing event in click event, thus on every click you are making new subscription.

Instead of this, you should be subscribing service's event once (before this click event, usually in load event of form or in constructor as per your convince).

But one thing you should keep in mind, this "service" object should be the same while subscribing event and calling API.

following link may clear this idea https://msdn.microsoft.com/en-us/library/ms366768.aspx

Amit
  • 1,821
  • 1
  • 17
  • 30
  • I have already accepted an answer :) Anyway, is it called `Event chaining`? – Huma Ali Feb 08 '16 at 06:33
  • yes, I have seen that you have accepted answer, but I wrote for answer to make sure people will read note of having "same object" (which is there in my answer. And we should not call it Event Chain. As Event Chaining is intentional (not accidental, as it was in your case). Event Chaining is very useful thing and easy to understand. follow the link [link]http://www.codeproject.com/Articles/27406/Event-Chain – Amit Feb 08 '16 at 06:50
  • Thanks for the clarification on Event chaining :) – Huma Ali Feb 08 '16 at 06:57