显示3个点加载动画时出现死循环
Endless Loop Happens when Displaying 3 Dots Loading Animation
我之前只是问过类似的问题,但现在,我尝试自己做这个,但它似乎没有按照我指定的那样工作。我编写了一种方法,可以在控制台应用程序(出现和消失的 3 个点)上打印加载动画,我决定在我正在使用的程序中实现它。
我需要做的是打印加载动画,直到进程完成,我看到了一些初始化新线程的方法(不能与我的方法一起使用,因为 ThreadStart 需要一个 Void 没有参数才能正常工作)。其他方法,只显示加载动画,直到用户按下任意键(Console.ReadLine() 方法),这不是我想要做的。
我自己的代码与我在网上看到的类似,但我做了一些更改,首先:它显示加载程序动画以及我希望在屏幕上打印的字符串,其次:它不初始化一个线程,因为当我启动一个线程时,它不能用 Thread.Abort().
取消
如果 Loader 动画被打印,那么,它永远不会结束,循环永远不会中断,强制程序停止工作。
这是我编写的代码,非常感谢任何帮助或建议:
我。三加载点法:
public static void Display_Loading_Dots(string String_to_Print, bool Action_is_Done)
{
while(Action_is_Done == false) // If Action is not Done, Process Display the Loading Animation it Until it is
{
Console.Write("{0}", String_to_Print);
char Dot = '.';
int Interval = 1000;
for(int Index = 0; Index < 3; Index++)
{
Console.Write(Dot);
Thread.Sleep(Interval);
if(Index == 2)
{
Console.Write("\b\b\b \b\b\b");
Index = -1;
Thread.Sleep(Interval);
}
}
}
if(Action_is_Done == true) // If the Action is Done, Stop Displaying the Dots
{
// This is Supposed to Break the for Block from the if Block, which is the One that Displays the Loading Animation
}
}
}
}
二.我试图停止三点动画但没有停止的示例:
string String_to_Print = "\nOpening the File \"" + Input_File_Name + "\"";
bool Action_is_Done = false;
// Let's Suppose, We just Opened the File and the Action its Done, so now, we have to Stop Displaying the Loading Animation
Process_Action(Action_String, Action_is_Done);
Action_is_Done = true; // Change the Boolean, to true, so the Loop will be Breaked
Process_Action(Action_String, Action_is_Done); // Does nothing, the Loop is not being Broken as Specified
我建议获取将点写出正常流程的代码
var x = new System.Threading.CancellationTokenSource();
var writeDotsTask = Task.Factory.StartNew(() => {
//TODO: Write your dots
}, x.Token);
//TODO: Load your stuff
x.Cancel();
甚至颠倒我个人更喜欢的逻辑
var doLoadTask = Task.Factory.StartNew(() => {
//TODO: Load your stuff
});
int approprateMillisecondWaitBetweenDotsDrawing = 500;
while(!doLoadTask.IsCompleted){
//TODO: Write your dots
Task.Delay(approprateMillisecondWaitBetweenDotsDrawing).GetAwaiter().GetResult();
}
我之前只是问过类似的问题,但现在,我尝试自己做这个,但它似乎没有按照我指定的那样工作。我编写了一种方法,可以在控制台应用程序(出现和消失的 3 个点)上打印加载动画,我决定在我正在使用的程序中实现它。
我需要做的是打印加载动画,直到进程完成,我看到了一些初始化新线程的方法(不能与我的方法一起使用,因为 ThreadStart 需要一个 Void 没有参数才能正常工作)。其他方法,只显示加载动画,直到用户按下任意键(Console.ReadLine() 方法),这不是我想要做的。
我自己的代码与我在网上看到的类似,但我做了一些更改,首先:它显示加载程序动画以及我希望在屏幕上打印的字符串,其次:它不初始化一个线程,因为当我启动一个线程时,它不能用 Thread.Abort().
取消如果 Loader 动画被打印,那么,它永远不会结束,循环永远不会中断,强制程序停止工作。
这是我编写的代码,非常感谢任何帮助或建议:
我。三加载点法:
public static void Display_Loading_Dots(string String_to_Print, bool Action_is_Done)
{
while(Action_is_Done == false) // If Action is not Done, Process Display the Loading Animation it Until it is
{
Console.Write("{0}", String_to_Print);
char Dot = '.';
int Interval = 1000;
for(int Index = 0; Index < 3; Index++)
{
Console.Write(Dot);
Thread.Sleep(Interval);
if(Index == 2)
{
Console.Write("\b\b\b \b\b\b");
Index = -1;
Thread.Sleep(Interval);
}
}
}
if(Action_is_Done == true) // If the Action is Done, Stop Displaying the Dots
{
// This is Supposed to Break the for Block from the if Block, which is the One that Displays the Loading Animation
}
}
}
}
二.我试图停止三点动画但没有停止的示例:
string String_to_Print = "\nOpening the File \"" + Input_File_Name + "\"";
bool Action_is_Done = false;
// Let's Suppose, We just Opened the File and the Action its Done, so now, we have to Stop Displaying the Loading Animation
Process_Action(Action_String, Action_is_Done);
Action_is_Done = true; // Change the Boolean, to true, so the Loop will be Breaked
Process_Action(Action_String, Action_is_Done); // Does nothing, the Loop is not being Broken as Specified
我建议获取将点写出正常流程的代码
var x = new System.Threading.CancellationTokenSource();
var writeDotsTask = Task.Factory.StartNew(() => {
//TODO: Write your dots
}, x.Token);
//TODO: Load your stuff
x.Cancel();
甚至颠倒我个人更喜欢的逻辑
var doLoadTask = Task.Factory.StartNew(() => {
//TODO: Load your stuff
});
int approprateMillisecondWaitBetweenDotsDrawing = 500;
while(!doLoadTask.IsCompleted){
//TODO: Write your dots
Task.Delay(approprateMillisecondWaitBetweenDotsDrawing).GetAwaiter().GetResult();
}