View Ravichandran J.V.'s profile on LinkedIn RJV 's Blog: May 2009 Tweet @ravichandranjv

Translate

Monday, 25 May 2009

Wait Cursor for console

Creating your own cursor for a processing job is always interesting - no matter what the environment is. Below is the code for creating one for console on windows:

using System;
using System.Threading;
public class WaitCursor{
public static void DrawCursor(){
int y=0;
Console.SetCursorPosition(Console.CursorLeft+15,Console.CursorTop+15);
for (int i=0;i<10000;i++){>0)
y=Console.CursorLeft-1;
Console.SetCursorPosition(y,Console.CursorTop);
Thread.Sleep(500);
Console.Write("\\");
if (Console.CursorLeft>0)
y=Console.CursorLeft-1;
Console.SetCursorPosition(y,Console.CursorTop);
Thread.Sleep(500);
Console.Write(""); // Use the Pipe key when you download. Somehow the font does not seem to

//display it here.
if (Console.CursorLeft>0)
y=Console.CursorLeft-1;
Console.SetCursorPosition(y,Console.CursorTop);
Thread.Sleep(500);
Console.Write("/");
if (Console.CursorLeft>0)
y=Console.CursorLeft-1;
Console.SetCursorPosition(y,Console.CursorTop);
Thread.Sleep(500);
Console.Write("-");
if (Console.CursorLeft>0)
y=Console.CursorLeft-1;
Console.SetCursorPosition(y,Console.CursorTop);
Thread.Sleep(500);
Console.Write("\\");
if (Console.CursorLeft>0)
y=Console.CursorLeft-1;
Console.SetCursorPosition(y,Console.CursorTop);
Thread.Sleep(500);
Console.Write(""); // Use the Pipe key when you download. Somehow the font does not seem to

//display it here.
if (Console.CursorLeft>0)
y=Console.CursorLeft-1;
Console.SetCursorPosition(y,Console.CursorTop);
}
}
public static void Main(){
DrawCursor();
}}

There are further avenues to the above example.

Instead of a loop and a sleeping thread, an asynchronous operation could be used for the cursor's rendering.

You can also use a functional programming approach where the previous symbol (\-/-\) could act as the input for the DrawCursor() function!