при частом обновлении окна консоли с помощью Console.Clear(); не видно текст, есть ли обходной || похожий способ изменить текст в консоли
в общем такая проблема: когда код часто обновляется не видно того, что выводится в эту консоль как можно изменить текст в ней без task.delay и прочих замедляющих код функций вот часть кода с проблемой:
//наглядная демонстрация загрузки// //с малыми числами работает плохо (не показывается)//
long[] loading = new long[4] { increasedvalue, 0, increasedvalue / 100, 0 };
long lastl1 = 0;
void loadingillustrtion()
{
char[] loadscreen = ("[__________]").ToCharArray(); ;
if (loading[1] != 0 || lastl1 != 0 && lastl1 != loading[1] || loading[3] != 0)
{
lastl1 = loading[1];
long count = loading[1] + 1;
int lcount = 1;
while (lcount != count)
{
loadscreen[lcount] = '#';
lcount++;
}
}
string releaseload = new string(loadscreen);
if (loading[1] != 10)
{
Console.Clear();
Console.WriteLine("\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t" + releaseload + loading[3] + "%");
}
else
{
Console.Clear();
Console.WriteLine("\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t" + releaseload + " complite!");
}
Ответы (1 шт):
Автор решения: aepot
→ Ссылка
Вот простое решение.
Создал класс, который будет отвечать за отображение прогрессбара.
public class ConsoleProgressBar
{
public int Left { get; set; }
public int Top { get; set; }
public int Length { get; set; }
public ConsoleProgressBar(int left, int top, int length)
{
Left = left;
Top = top;
Length = length;
}
public void ShowProgress(int progress)
{
if (progress > Length || progress < 0)
throw new ArgumentException($"Invalid progress value, must be between 0 and {Length} but actual {progress}.");
(int left, int top) = Console.GetCursorPosition();
Console.SetCursorPosition(Left, Top);
Console.Write($"{new string('▓', progress)}{new string('░', Length - progress)}");
Console.SetCursorPosition(left, top);
}
}
Написал вот такой вот тест
static void Main(string[] args)
{
Console.Clear();
string title = "Progress: ";
Console.WriteLine(title);
int maxProgress = 40;
ConsoleProgressBar bar = new ConsoleProgressBar(title.Length, 0, maxProgress);
long previous = -1;
long total = 1000000000;
for (long i = 0; i < total; i++)
{
long progress = i * maxProgress / total;
if (progress != previous)
{
bar.ShowProgress((int)progress);
previous = progress;
}
}
bar.ShowProgress(maxProgress);
Console.WriteLine("Complete.");
Console.ReadKey();
}
Выглядит это вот так.
Progress: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░
Никаких пауз, как вы и просили, но перерисовка прогрессбара происходит только тогда, когда это необходимо, то есть его состояние поменялось. В данном конкретном случае вывод в консоль внутри цикла произойдет ровно 40 раз.