✅ Fake loading bar

I'm trying to make a fake loading bar but it's not printing the bar in the same line code:
using System;
using System.Threading;

namespace FakeLoading
{
internal class FakeLoad
{
public static void Main(string[] args)
{
for(int i = 0; i < 20; i++)
{
Console.Write($"\rLoading : [{new string('#', i)}{new string('.', 20 - i)}]");
Thread.Sleep(100);
}
}
}
}
using System;
using System.Threading;

namespace FakeLoading
{
internal class FakeLoad
{
public static void Main(string[] args)
{
for(int i = 0; i < 20; i++)
{
Console.Write($"\rLoading : [{new string('#', i)}{new string('.', 20 - i)}]");
Thread.Sleep(100);
}
}
}
}
No description
13 Replies
Pobiega
Pobiega2w ago
Why would it? you never tell it to reset the console cursor position
Pobiega
Pobiega2w ago
Explore what properties and methods exist on Console and you'll probably figure it out 🙂
Anar
Anar2w ago
carriage return character (\r) moves the cursor back to the start of the line if it is an interactive shell from what I can see, the output window you have is "redirected" instead I might be wrong though
DidierThePenguin 🇫🇷
I tried Console.SetCursorPosition(); to test but it does this
No description
Anar
Anar2w ago
so '\r' is ignored, anything you print is simply redirected to your console and printed sequencially yeah, looks like you indeed don't have a real console buffer attached you can test it with
Console.WriteLine(Console.IsOutputRedirected);
Console.WriteLine(Console.IsOutputRedirected);
should print true so the problem is not with your code, but with your environment
DidierThePenguin 🇫🇷
I did crtl + F5 instad of F5 it was working damn it's working now
DidierThePenguin 🇫🇷
but I got 2 times the same thing
No description
Anar
Anar2w ago
good :D
MODiX
MODiX2w ago
If you have no further questions, please use /close to mark the forum thread as answered
Anar
Anar2w ago
wait, I missed this message, not sure I understand what's happening here
DidierThePenguin 🇫🇷
I changed I'm in Visual Studio instead it's working well

Did you find this page helpful?