我有下面的代码和问题

step1.Visible = true;
//step1 is visible if i retrun from here but if do some work like below than its not visible until the task is completed
Thread.Sleep(5000); // some opration here  Thread.Sleep is just for example
step1.Visible = true;// step1 visible here not before thread going to sleep


在这里,我想显示每个步骤的图像,但是第一步图像不显示它是否跟随着一些长期运行的任务,在Thread.Sleep(5000)的情况下,任何想法/技巧都显示step1?

最佳答案

在睡眠(任何长时间运行)代码之前使用Application.DoEvents()
Application.DoEvents()将处理消息队列中当前的所有Windows消息。

step1.Visible = true;

// Below 3 lines are not necessary. Use it only if DoEvents() doesn't work.
//step1.Invalidate();
//step1.Update();
//step1.Refresh();

// Will process the pending request of step1.Visible=true;
Application.DoEvents();

Thread.Sleep(5000);
step1.Visible = true;

09-27 20:33