user1667191
user1667191

Reputation: 2537

Listview double click not working correctly

I have a problem with this listivew. When I double click it, it should show me a dialog of info of the 1st cell of the row I click and that works perfectly fine.

Now the problem is that if I keep clicking rows, I still get the dialogs from the previous rows I've clicked.

I click a random row 1 time: Shows me dialog
I click another random row: Shows me dialog from the first row I clicked, then when I close said dialog it shows me the dialog of info of the row I clicked.
I click another random row: Shows me dialog from the first row I clicked and from the second one.

.. and it keeps going

Here's the code:

    private void listViewPlayers_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        string
            pUser = "";

        int
            pID = -1;

        pID = Convert.ToInt32(listViewPlayers.SelectedItems[0].SubItems[0].Text);
        pUser = listViewPlayers.SelectedItems[0].SubItems[1].Text;

        bw.WorkerReportsProgress = true;

        bw.DoWork += new DoWorkEventHandler
        (
            delegate(object o, DoWorkEventArgs args)
            {
                BackgroundWorker b = o as BackgroundWorker;

                pInfo = pInfoClient.DownloadString("get info from web").Trim();
            }
        );

        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler
        (
            delegate(object o, RunWorkerCompletedEventArgs args)
            {
                if (strcmp(pInfo, "ERROR"))
                {
                    MessageBox.Show("There was an error retrieving user information!\nPlease try again later..", "Error");
                    return;
                }

                string[]
                    iSplit = pInfo.Split(new char[] { (char)'|' }, System.StringSplitOptions.RemoveEmptyEntries);

                playerInfo iInfo = new playerInfo(pUser, pID, iSplit);

                iInfo.ShowDialog();
            }
        );

        bw.RunWorkerAsync();
    }

And excuse my explanation of the problem, I'm terrible at it.

Upvotes: 0

Views: 651

Answers (1)

Vassi
Vassi

Reputation: 780

You may have an inadvertent memory leak situation.

It looks like "bw" is a class variable? If so, your click event is constantly adding new handlers to the background worker's events but is never removing the handlers so every time your mouse click event fires it tells the background worker to do it's thing and those events trigger for all of the previous handlers you've ever registered.

You could try newing up the background worker every time, which should resolve the issue, or in the DoWork event you may want to ensure that you de-register the delegate. I.E.

var inlineHandler = new DoWorkEventHandler ( delegate(object o, DoWorkEventArgs args) 
{
    BackgroundWorker b = o as BackgroundWorker;
    pInfo = pInfoClient.DownloadString("get info from web").Trim();
    bw.DoWork -= inlineHandler;

});
bw.DoWork += inlineHandler;

I would highly recommend newing up the worker every time though.

Upvotes: 1

Related Questions