开发者

Winform ListViewItem Checked Status Toggle

I have a problem with race conditions with winforms.

  listViewCollections.ItemChecked += foo //Somewhere

  void foo(object sender, ItemChecke开发者_运维技巧dEventArgs e)
    {
        if (e.Item.Checked == false) return;
        bool flag = bar( e.Item.Index);
        if (flag) {
            MessageBox.Show( ... )
            e.Item.Checked = false;
       }
    }

Sometimes the message box shows twice. What's the correct way to do this? Thanks in advance!


Could you not just put a simple lock around it? Also I would suggest switching the way the Checked logic works to simplify the function a little (remove the mid-function return)

private static object fooLock = new object();
void foo(object sender, ItemCheckedEventArgs e)
{
    lock (fooLock)
    {
        if (e.Item.Checked) 
        {
            if (bar(e.Item.Index)) 
            {
                MessageBox.Show( ... )
                e.Item.Checked = false;
            }
        }
    }
}

Numerous ways to improve the performance, but that should at least stop the race conditions?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜