简体   繁体   中英

Windows form show hide control not working as expacted

I have my code and problem like below

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

Here I want to show the image for each step but first step image not showing if it's followed by some long running task any idea/trick show step1 in the case of Thread.Sleep(5000)?

Use Application.DoEvents() before your Sleep (Any long running) Code. Application.DoEvents() will processes all Windows messages currently in the message queue.

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;

I'm not sure if I understand what do you want to do but first I will use those two function : Show() and Hide()

Now you have to work with Multi Threading and comunication between Threads

Hope this could help

You should use delegates or backgroundworker process to achieve that. you can get and idea from the link below.

Animated GIF in Windows Form while executing long process

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM