Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am trying to make a breathing effect but my form's ForeColor is not changing at all. Could anyone help? My code for the
C#
Breathe();
function is.

C#
private async void Breathe() {

    for (int i = 0; i > 40; i++)
    {
        this.ForeColor = Color.FromArgb(this.ForeColor.R, this.ForeColor.G + i, this.ForeColor.B + 1);
        await Task.Delay(10);
    }
    await Task.Delay(1000);
    for (int i = 0; i > 40; i++)
    {<a href=""></a>
        this.ForeColor = Color.FromArgb(this.ForeColor.R, this.ForeColor.G - i, this.ForeColor.B - 1);
        await Task.Delay(10);
    }

}



If you have ever installed Windows 10 and [seen this screen], That's what I'm trying to mimic

What I have tried:

Using for loops to fade in and out from a darker color to a brighter color then back.
Posted
Updated 3-Jan-22 11:25am

How many time do you think this for (int i = 0; i > 40; i++) will loop?

The for little bit more in detail:
a.) We start here: i= 0;
b.) We proceed as long condition is true: Do it as long i > 40; ..... think about this....

Is i ever > 40? No, because you start with it at zero and therefore nothing will happen ;)

Wild guess, maybe you mean this:
for (int i = 0; i < 40; i++)
 
Share this answer
 
v2
Comments
j ongamer13 3-Jan-22 16:38pm    
Sometimes '<' and '>' get inverted lol, Also I keep on getting 257 or -1 as RGB values as a exception.
0x01AA 3-Jan-22 16:40pm    
So it is ;) Thank you for accepting.
j ongamer13 3-Jan-22 16:48pm    
Still, the ForeColor isn't changing, the loop should work, I added if statements to prevent exceptions but that still doesn't work

I tried inverting < and > but to me it does nothing
0x01AA 3-Jan-22 16:54pm    
a.) 40 Steps each 10mS is 400mS, around 0.5 seconds ... maybe to fast
b.) I assume you corrected at least the first for. Now how your secnd for looks like?
c.) Has the GUI a chance to update with this asynch stuff?
j ongamer13 3-Jan-22 16:55pm    
I inverted ForeColor with BackColor that was my problem
In addition to what 0x01AA has said, that won't work because your form never gets to paint the changed form - that's not how things work.
There is one thread that interacts with the user - displaying information and accepting his input - called the UI thread, and it's the thread that starts when your application does. If at any point you sit in a loop in an event handler (and every action in your app is as a response to an event and hence in an event handler) then no other events are processed until that event handler returns - including the Paint event handler which handles updating the actual display.

So your code is effectively sitting there doing nothing for a short amount of time and then painting the form the colour it was to start with! There are ways round that, but they either involve you using a Timer and changing the colour once each time it Ticks, or a second thread to do that. And that gets complicated because only the UI thread can access any Control (and a Form is a Control) - any attempt to do so will immediately cause a cross threading exception. So you have to either use a BackgroundWorker and send the request back to the UI thread via the Progress reporting mechanism, or use a standard thread and Invoke the changes back to the UI thread.
It gets complicated, doesn't it?

But there is worse to come! Setting the foreground colour on a Form doesn't do anything like you want - because it isn't automatically "inherited" by the child controls so none of them get updated. And since they are the only ones with any foreground data on them in the form of Text and so on, you probably wouldn't see any changes anyway ... which means you need to recursively process the whole Controls collection to update every Control on every Control on the Form in order to see anything happen.

Are you sure you want to go down this rabbit hole? It gets quite deep!
 
Share this answer
 
Comments
j ongamer13 3-Jan-22 21:39pm    
I already fixed the problem, I needed to add if statements to prevent exceptions, invert "for" statement and change ForeColor to BackColor and thats mostly it
Richard Deeming 5-Jan-22 6:10am    
The await Task.Delay(...) calls return control to the UI thread, which will allow the form to paint. Behind the scenes, it essentially creates a timer which will resume the method on the UI thread after the specified time has elapsed.

Of course, you should still avoid async void methods[^]. :)

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