解决控制台刷新问题

This commit is contained in:
muqing 2024-08-26 07:51:27 +08:00
parent bb007149b8
commit 6a261ecae7
4 changed files with 191 additions and 40 deletions

85
MinLong/KongLong.cs Normal file
View File

@ -0,0 +1,85 @@
using System.Diagnostics;
namespace Debug.MinLong;
public class KongLong
{
public KongLong()
{
Console.WriteLine("MinLong");
new Thread(PrintCharArray).Start();
}
public char[,] chars = new char[10, 30];
private long time;
public void PrintCharArray()
{
var stopwatch = new Stopwatch();
int frameCount = 0;
double totalTime = 0;
int i; int j;
var a = chars.GetLength(0);
var b = chars.GetLength(1);
while (Program.IsWhile)
{
stopwatch.Restart();
Console.Clear();
Console.SetCursorPosition(0, 0);
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
// Console.ForegroundColor = chars[i, j].Color;
// Console.Write($" {chars[i, j].Char} ");
Console.Write(chars[i, j]);
time++;
if (time > 100000)
{
GC.Collect(); time = 0;
}
}
Console.WriteLine();
}
Console.ForegroundColor = ConsoleColor.White;
for (i = 0; i < b; i++)
{
Console.Write("---");
}
Console.ResetColor();
// 停止计时并计算本帧的处理时间
stopwatch.Stop();
double elapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds;
totalTime += elapsedMilliseconds;
frameCount++;
// 计算帧率
double fps = frameCount / (totalTime / 1000.0);
Console.SetCursorPosition(0, 0);// 设定光标位置到第二行
Console.WriteLine($" {fps:0} FPS");
// 保持每秒约16.67ms即大约60 FPS的间隔
// Thread.Sleep(Math.Max(0, 60 - (int)elapsedMilliseconds));
//死方法 不知道为什么 控制台限制60 帧这样闪烁不会太严重
Thread.Sleep(100);
}
}
private void SetChars()
{
for (int i = 0; i < chars.GetLength(0); i++)
{
for (int j = 0; j < chars.GetLength(1); j++)
{
chars[i, j] = '0';
}
}
}
}

View File

@ -1,9 +1,11 @@
using System.Diagnostics;
using System.Text;
namespace Debug.ModelingMusic;
public class Music
{
public static bool IsWhile = true;
//选择的模式
public static int mode = 0;
@ -30,16 +32,17 @@ public class Music
} while (true);
}
private int consoleWidth = 0, consoleHeight = 0;
private async Task Init()
{
Console.CursorVisible = false; // 隐藏光标
Console.Clear();
Program.printf("加载资源中");
// Console.SetWindowSize(chars.GetLength(0) * 5, chars.GetLength(1) * 5); // 设置窗口大小
// Console.SetBufferSize(chars.GetLength(0) * 5, chars.GetLength(1) * 5); // 设置缓冲区大小
// 获取控制台的大小
consoleWidth = Console.WindowWidth;
consoleHeight = Console.WindowHeight - 1;
// consoleWidth = Console.WindowWidth;
// consoleHeight = Console.WindowHeight - 1;
for (int i = 0; i < 3; i++)
{
Program.printf("·");
@ -88,8 +91,24 @@ public class Music
}
Console.CursorVisible = false; // 隐藏光标
new Thread(PrintCharArray).Start();
if (mode == 1) { return; }
new Thread(() =>
{
while (IsWhile)
{
ConsoleKeyInfo consoleKeyInfo = Console.ReadKey(true);
if (consoleKeyInfo.Key == ConsoleKey.Escape)
{
IsWhile = false;
Thread.Sleep(500);
Console.Clear();
Environment.Exit(0);
return;
}
Thread.Sleep(500);
}
}).Start();
// 等待用户按下任意键后退出
// Console.ReadKey();
}
@ -103,6 +122,7 @@ public class Music
public const char ShowChar = ' ';
//Y,X
public static readonly Point[,] chars = new Point[10, 30];
// public static readonly char[,] chars = new char[10, 30];
public static void Down(char a)
{
// 随机生成字符出现的初始位置
@ -137,7 +157,7 @@ public class Music
Thread.Sleep(50);
chars[chars.GetLength(0) - 1, x].Char = ShowChar;
}
private static long item = 0;
private static long time = 0;
// 定义一个方法来打印二维字符数组
public static void PrintCharArray()
@ -149,43 +169,43 @@ public class Music
int i; int j;
var a = chars.GetLength(0);
var b = chars.GetLength(1);
while (true)
var buffer = new StringBuilder();
while (IsWhile)
{
stopwatch.Restart();
Console.Clear(); Console.SetCursorPosition(0, 0);
Console.SetCursorPosition(0, 0);// 设定光标位置
Console.Clear();
buffer.Clear();
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
Console.ForegroundColor = chars[i, j].Color;
Console.Write($" {chars[i, j].Char} ");
item++;
if (item > 100000)
//Console.Write($" {chars[i, j].Char} ");
buffer.Append(chars[i, j].Char);
time++;
if (time > 100000)
{
GC.Collect(); item = 0;
GC.Collect(); time = 0;
}
}
Console.WriteLine();
buffer.AppendLine();
//Console.WriteLine();
}
switch (new Random().Next(0, 2))
Console.Write(buffer.ToString());
if (time % 1000 == 0)
{
case 0:
Console.ForegroundColor = ConsoleColor.Red;
break;
case 1:
Console.ForegroundColor = ConsoleColor.Blue;
break;
Console.ForegroundColor = random.Next(0, 2) == 0 ? ConsoleColor.Red : ConsoleColor.Blue;
}
// Console.ForegroundColor = (ConsoleColor)random.Next(0, 16);
for (i = 0; i < b; i++)
{
Console.Write("---");
Console.Write("-");
}
Console.ResetColor();
Console.SetCursorPosition(0, 1);// 设定光标位置到第二行
// 停止计时并计算本帧的处理时间
stopwatch.Stop();
@ -194,13 +214,12 @@ public class Music
frameCount++;
// 计算帧率
double fps = frameCount / (totalTime / 1000.0);
Console.WriteLine($" {fps:0} FPS");
// 保持每秒约16.67ms即大约60 FPS的间隔
// Thread.Sleep(Math.Max(0, 60 - (int)elapsedMilliseconds));
//死方法 不知道为什么 控制台限制60 帧这样闪烁不会太严重
Thread.Sleep(100);
Thread.Sleep(150);
}
}
private void setCharArry()

View File

@ -26,7 +26,7 @@ public class MusicDebug
{
new Thread(() => Music.Down(keyChar)).Start();
}
} while (true);
} while (Music.IsWhile);
}
/// <summary>
@ -38,7 +38,7 @@ public class MusicDebug
Console.CursorVisible = false; // 隐藏光标
var random = new Random();
while (true)
while (Music.IsWhile)
{
//随机生成 a-z和A-Z
var ch = (char)(random.Next(26) + 65);
@ -57,7 +57,7 @@ public class MusicDebug
Console.Clear();
var random = new Random();
while (true)
while (Music.IsWhile)
{
for (int v = 0; v < ch.Length; v++)
{
@ -75,7 +75,7 @@ public class MusicDebug
Console.Clear();
var random = new Random();
while (true)
while (Music.IsWhile)
{
int v = random.Next(0, ch.Length);
new Thread(() => Music.Down(ch[v])).Start();
@ -87,14 +87,15 @@ public class MusicDebug
public static void MusicRhythm()
{
WasapiLoopbackCapture cap = new WasapiLoopbackCapture();
var cap = new WasapiLoopbackCapture();
cap.DataAvailable += (sender, e) =>
{
// Console.Clear();
// Console.WriteLine(e.BytesRecorded + " " + e.Buffer.Length);
float[] allSamples = Enumerable
.Range(0, e.BytesRecorded / 4)
.Select(i => BitConverter.ToSingle(e.Buffer, i * 4))
.ToArray();
int channelCount = cap.WaveFormat.Channels;
float[][] channelSamples = Enumerable
.Range(0, channelCount)
@ -113,15 +114,21 @@ public class MusicDebug
.ToArray();
float rmsVolume = CalculateRMS(averageSamples);
//Console.Clear();
// Console.WriteLine("RMS Volume: " + rmsVolume);
DrawVolumeBar(rmsVolume);
//Console.WriteLine($"RMS Volume: {rmsVolume}");
Thread.Sleep(100);
// Thread.Sleep(1000);
};
cap.StartRecording();
Console.ReadKey();
// new Thread(() =>
// {
// while (Music.IsWhile)
// {
// // Console.Clear();
// // Console.WriteLine("RMS Volume: " + rmsVolume);
// Thread.Sleep(100);
// }
// }).Start();
}
static float CalculateRMS(float[] samples)
@ -133,11 +140,8 @@ public class MusicDebug
static void DrawVolumeBar(float rmsVolume)
{
// Console.Write(rmsVolume);
int barLength = Music.chars.GetLength(1); // 控制条长度
int filledLength = Math.Min((int)(rmsVolume * 1000), barLength);
//Console.WriteLine($"[{bar}]");
// 在数组的最后一行Y=9绘制音量条
for (int x = 0; x < Music.chars.GetLength(1); x++)
{
Music.chars[9, x].Color = ConsoleColor.DarkCyan;

View File

@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using Debug.ModelingMusic;
namespace Debug;
@ -28,10 +29,52 @@ public class Program
{
// 设置控制台标题为一个独特的字符串
SetConsoleTitle("Tools");
// 设置控制台输出编码,以支持复杂字符
Console.OutputEncoding = Encoding.UTF8;
//// 主循环
//while (true)
//{
// // 创建一个缓冲区
// char[,] buffer = new char[height, width];
// // 在缓冲区中绘制内容
// for (int y = 0; y < height; y++)
// {
// for (int x = 0; x < width; x++)
// {
// buffer[y, x] = (x + y) % 2 == 0 ? '#' : ' ';
// }
// }
// // 将缓冲区的内容输出到控制台
// Console.SetCursorPosition(0, 0);
// Console.Clear();
// StringBuilder sb = new StringBuilder();
// for (int y = 0; y < height; y++)
// {
// for (int x = 0; x < width; x++)
// {
// sb.Append(buffer[y, x]);
// }
// sb.AppendLine();
// }
// Console.Write(sb.ToString());
// // 控制刷新率
// Thread.Sleep(200);
//}
_ = new Program();
}
public static bool IsWhile = true;
public Program()
{
// new Thread(MusicDebug.MusicRhythm).Start();
// return;
// new Debug.MinLong.KongLong();
// return;
do
{
printfLine("请选择你要执行的操作:"); printf(" 0.退出 ");