用doevent刷新()听 [英] Refresh with doevent () Listen

查看:68
本文介绍了用doevent刷新()听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用了在运行时刷新doevent标签文本的代码.
doevent是如何工作的?
-------------------------------------------------- --------------

The code for the doevent label text refresh in run-time is used.
How it works doevent?
----------------------------------------------------------------

private void button1_Click(object sender, EventArgs e)
        {
           
            int i=0;
            label1.Text = "start loop1";
            while (i<100000000)
            {
              i++;
            }

            label1.Text = "start doevent";
            Application.DoEvents();      // refresh
                                   
            System.Threading.Thread.Sleep(10000);    
        
            label1.Text = "start loop2";
            Application.DoEvents();
            i = 0;
            while (i < 1000000000)
            {
              
                i++;
            }

         label1.Text = null;
         label1.Text = "end";
        
        }

推荐答案

经验法则:请勿使用DoEvents.另外,从不在UI线程中等待(这最后一条语句不是经验法则" —永远不会).
请查看我以前的答案:它显示了单击按钮时的操作:

逐步更改以显示控件 [ ^ ].

如此多的开发人员使用DoEvents会犯同样的错误.这很容易理解发生了什么.您需要一些动画行为.如果您在一个UI线程中编写代码,则始终会阻止该线程进行事件处理.调用DoEvents时,只需等待所有事件都通过UI传递即可.只有第二个线程可以有效地执行您想要的操作.它显示在我以前的答案中.

—SA
A rule of thumb: never use DoEvents. Also, never wait in UI thread (this last statement is not a "rule of thumb" — just never ever).
Please see my previous answer: it shows what to do on button click:

Changes step by step to show a control[^].

So many developers make the same mistake using DoEvents. This is easy to understand what''s going on. You need some animated behavior. If you write in in one UI thread, you always block this thread from processing of events. When you call DoEvents, you simply wait until all events are passing through UI. Only a second thread can do what you want effectively. It''s shown on my previous answer.

—SA


这篇关于用doevent刷新()听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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