Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to interactively change button properties in the UI visually.

I have a sole button in my Windows Forms application and this is my code for the button click

C#
private void button1_Click(object sender, EventArgs e)
        {

            button1.BackColor = Color.Red;
            button1.Text = "1";
            Thread.Sleep(2000);
            button1.BackColor = Color.Purple;
            button1.Text = "2";

            Thread.Sleep(2000);
            button1.BackColor = Color.White;
            button1.Text = "3";
        }



However, during the Thread Sleep operations, the button is in its default color and it changes to its final color only when the whole code has finished running. What am I doing wrong here ? Is this the way that threads work ?

When i checked keeping debugger, the property is getting set correctly, but its like the changes are submitted to the UI only when the whole code finishes executing. Is there a way to update the UI dynamically.

My aim is to change the color and size of the button multiple times and those changes should get reflected in the UI
Posted
Updated 27-Mar-14 2:42am
v2

Simple: don't use Sleep at all.

Sleep suspends the current thread, so it does absolutely nothing until the time period has expired. Since it is the UI thread (or you wouldn't be in the Click event handler) that means that the thread that updates the display is suspended - so the display won't get updated until the Click even handler ends - which is after you have changed it again, and suspended it again!

If you really need to do this, then set up a Timer, with an Interval of (say) 100 (or 1/10th of a second) and set a "change to Purple" counter to 20. In the Timer Tick event handler, check the counter, and if it is non-zero reduce it by one. When it reaches zero, change the button properties. (And set a "change to White" counter - which you handle in the same way in the same Tick event handler).

Sounds complex, but it's pretty simple realy.
 
Share this answer
 
Comments
Raul Iloc 27-Mar-14 8:53am    
Your solution is OK but your comment is partially true, because simple Refresh() is working (he just tested sucesfully my solution)!
OriginalGriff 27-Mar-14 9:03am    
Yes - because your "solution" at the time involved just adding more Sleep, and possibly calling Invoke from the UI thread...
Neither of which would have worked - if you had actually tried it, you would have found out! :laugh:
Raul Iloc 27-Mar-14 9:19am    
At the beggining I didn't sow that are 2 Sleeps calls in the source code provided, but I didn't sugested to use Invoke!
OriginalGriff 27-Mar-14 9:28am    
Ahem:
"You should invoke Thread.Sleep(2000);"
(If you look at your answer, you will see a little "v4" - click on it and it lets you see the whole history)
Raul Iloc 27-Mar-14 9:35am    
Like I said in my comment above "at the beggining I didn't sow that are 2 Sleeps calls" and there I suggested to move the existing call to "Sleep(2000);" at the end of the method.
You should invoke button1.Refresh() after each change of color, but before Thread.Sleeep()!
 
Share this answer
 
v4
Comments
Divakar Raj M 27-Mar-14 8:44am    
I didn't get you. I tried doing that but it didnt make any difference. What I am trying for is, in the thread sleep time, my button should be displayed as "Red" and "Purple" respectively. It is currently not doing that
Raul Iloc 27-Mar-14 8:47am    
Did you try button1.Refresh() and didn't work?
Divakar Raj M 27-Mar-14 8:49am    
That worked. Thank you !! :)
OriginalGriff 27-Mar-14 8:51am    
Reason for my vote of one: Did you try that, at all?
Do you know what Invoke does?
Raul Iloc 27-Mar-14 9:10am    
The user wanted some simple changes of the color and text after the user click with some delays between changes and keeping the user input in wait stage. The usage of timer was the single solution in MFC (old times) but in C# a simple Refresh() is OK for this request.

PS: In generally the best solution are the simple ones, and we do not have to complicate things when is not necessary.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900