Search code examples
c#windows-phone-7event-handling

Multiple event handler firings, why?


I have a tough time trying to solve this problem. I have been at for 3 hours, and still I couldn't find out why it is doing this. Here is the code:

private void Catagory_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int selectedCategoryId = categoryIdList[categoryListBox.SelectedIndex];

        client.GetItemsAsync(selectedCategoryId);
        client.GetItemsCompleted += 
            new EventHandler<GetItemsCompletedEventArgs>(client_GetItemsCompleted);
    }

void client_GetItemsCompleted(object sender, GetItemsCompletedEventArgs e)
{
        itemIdList.Clear();
        itemNameList.Clear();
        itemNumberList.Clear();
        itemDisplayList.Clear(); //Clears the Display List Items

        if (e.Error == null)
        {
            itemIdList = e.ItemIDList;
            itemNumberList = e.itemNumber;
            itemNameList = e.Result;

            for (int i = 0; i < itemIdList.Count; i++)
            {
                itemDisplayList.Add(new ItemDisplay { itemNumber = itemNumberList[i], itemName = itemNameList[i] });
            }

            //Populating the listbox controll with the itemDisplaylist...
            Items.ItemsSource = itemDisplayList;
        }
        else
        {
            MessageBox.Show("Problem in getting the items list.");
        }
    }

When I change the category the first time it works perfectly. By perfectly, I mean that it calls the function GetItemsAsync(selectedCategoryId) and grabs the results and calls the event handler client_GetItemsCompleted() and the inner working of the event handler works as it is supposed to, it sets the lists with the proper data and displays the itemNumber and the itemName in the list box . But, when I change the category again to get different items, it doesn't work properly, what it's doing is that it clears the lists and populates the lists as it is supposed to, runs the for loop and populates the listBox called Items but for some reason it goes to the top of the function again and empties all the lists. Please tell my why it is executing the function again? And when I choose another category again, it executes the event handler 3 times and then 4 times and so on. Wnow why it is doing this?


Solution

  • Everytime this is executed:

      client.GetItemsCompleted += 
    

    You add a subscriber to the event, so the second time it will fire twice (the third time triple times, etc..).

    Either unsubscrice ( -= ) in the completed method:

    void client_GetItemsCompleted(object sender, GetItemsCompletedEventArgs e)
    {
        try {
           /* .... */
        }
        finally {
            client.GetItemsCompleted -= 
                new EventHandler<GetItemsCompletedEventArgs>(client_GetItemsCompleted);
        }
    }
    

    or initiate the client object before every call.

    var client = new ...();
    client.GetItemsAsync(selectedCategoryId);
    client.GetItemsCompleted += 
                new EventHandler<GetItemsCompletedEventArgs>(client_GetItemsCompleted);