jitender
jitender

Reputation: 10429

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)?

Upvotes: 1

Views: 380

Answers (3)

Anthony Fasano
Anthony Fasano

Reputation: 71

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

Upvotes: 1

Habeeb
Habeeb

Reputation: 8007

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;

Upvotes: 2

Abhishek K. Upadhyay
Abhishek K. Upadhyay

Reputation: 1032

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

Upvotes: 0

Related Questions