0

我有这个listivew的问题。当我双击它时,它应该向我显示我单击的行的第一个单元格的信息对话框,并且效果很好。

现在的问题是,如果我继续单击行,我仍然会从我单击的先前行中获得对话框。

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.

..它继续

这是代码:

    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();
    }

请原谅我对问题的解释,我对此很糟糕。

4

1 回答 1

1

您可能会遇到无意的内存泄漏情况。

看起来“bw”是一个类变量?如果是这样,您的单击事件会不断向后台工作人员的事件添加新的处理程序,但不会删除处理程序,因此每次您的鼠标单击事件触发时,它都会告诉后台工作人员执行此操作,并且这些事件会为您之前的所有处理程序触发'曾经注册过。

您可以尝试每次都更新后台工作人员,这应该可以解决问题,或者在 DoWork 事件中您可能希望确保取消注册代理。IE

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;

不过,我强烈建议每次都更新工人。

于 2013-10-16T19:54:57.217 回答