如何使更多对象为null [英] How to make more objects null

查看:60
本文介绍了如何使更多对象为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个按钮。每当我按下其中任何一个时,表格就会闪烁并被冻结。更多次我点击按钮,它需要更长的冻结时间。我在互联网上搜索并找到Controls.Clear();是从控件数组清除,而不是从内存中清除。我试图清除记忆,但我意识到我不知道该怎么做。我尝试了它,并为2个版本的循环得到了2个错误。

I have 3 buttons. Every time i press on either of them, the form is flickering and is frozen. More times i click on the buttons, the LONGER the frozen time it takes. I searched on internet and find that Controls.Clear(); is cleaning from array of controls but not from memory. I tried to clear the memory, but i realize i have no idea how to do it. I tried it and got 2 errors for 2 versions of loop.

        private void button01_Click(object sender, EventArgs e)
        {
            Controls.Clear();            InitializeButtons();
            // ClearALLControls();
        } 

        private void button02_Click(object sender, EventArgs e)
        {
            Controls.Clear();            InitializeButtons();
        }

        private void button03_Click(object sender, EventArgs e)
        {
            Controls.Clear();            InitializeButtons();
        }


        void ClearALLControls() //this method not working - got 2 errors
        {
            foreach (Control item in this.Controls)
            {
                item = null; //error@ [item]
//Cannot assign to 'item' because it is a 'foreach iteration variable'	

            }
//so... it's a foreach problem? i try it with for loop then

            for (int i = 0; i < Controls.Count; i++)
            {
                Controls[i] = null; //error@ [Controls[i]]
//System.Windows.Forms.Control.ControlCollection.this[int]' cannot be assigned to -- it is read only	
            }
        }





我尝试过:



......................................



What I have tried:

......................................

推荐答案

猜测,这是你的InitializeButtons正在造成伤害。

我怀疑你是在现有按钮上添加点击处理程序并将它们放回Controls集合中。 br />
并且处理程序是链式委托:两次添加相同的处理程序意味着它被调用两次,然后三次四次,五次,...所以处理程序执行所花费的时间每次增加按下。



你在那里展示的东西并没有表现出来 - 这就是猜测的原因 - 但是你展示的代码是......嗯......奇。看起来你不知道自己在做什么,并按照命中和希望进行编码,而不是仔细考虑,因为我无法弄清楚你认为你想要做的是什么,代码可能是好的解决方案!
At a guess, it's your InitializeButtons that is doing the damage.
I suspect that you are adding click handlers to the existing buttons and putting them back in the Controls collection.
And handlers are chained delegates: adding the same handler twice means it's called twice, then three times four times, five times, ... so the amount of time the handler takes to execute increases each time it is pressed.

What you show there doesn't show that bit - that's why it's a guess - but the code you do show is ... um ... rather odd. It looks like you don't know what you are doing, and are coding by "hit and hope" rather than thinking it through carefully, because I can't work out what you think you are trying to do that that code might be good solution to!


这篇关于如何使更多对象为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆