I just asked something similar before, but now, I tried to do this on my own with, but it seems to not being working as I specified. I've written a method which prints a Loading Animation on a Console Application (the 3 Dots that appear and disappear) and I've decided to implement it on my program I'm working for.
What I need to do is to print the Loading Animation until a Process is Finished, and I saw some methods that Initializes a new Thread (which can't be used with my methods because, ThreadStart needs a Void with no Parameters to work properly). Other methods, just display the Loading Animations until the User Press any Key (Console.ReadLine() Method) and that is not what I'm desired to do.
My own Code is similar from the Ones from Internet I saw, but I made some changes, first: it Displays the Loader Animation along with the String I'm desired to Print onscreen, and second: it does not Initializes a Thread, because, when I start a Thread it cannot be Cancelled with Thread.Abort().
If the Loader Animation is Printed, then, it'll never end, and the Loop will never break, Forcing the Program to Stop working.
Here's my Code I wrote, any help or Advice is very Appreciated:
I. Three Loading Dots Method:
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
}
}
}
}
II. An Example where I'm trying to Stop the Three Dots Animation, but it doesn't:
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
CodePudding user response:
I would suggest getting the code that writes the dots out of your normal flow
var x = new System.Threading.CancellationTokenSource();
var writeDotsTask = Task.Factory.StartNew(() => {
//TODO: Write your dots
}, x.Token);
//TODO: Load your stuff
x.Cancel();
or even invert the logic, which i personally prefer
var doLoadTask = Task.Factory.StartNew(() => {
//TODO: Load your stuff
});
int approprateMillisecondWaitBetweenDotsDrawing = 500;
while(!doLoadTask.IsCompleted){
//TODO: Write your dots
Task.Delay(approprateMillisecondWaitBetweenDotsDrawing).GetAwaiter().GetResult();
}