How to add an overwriting message to a console application in c#.

As part of my TunerFreeMCE application, I have a console application which builds up a cache of all of the programs that can be watched. That can be a slow process at times, particularly on hulu and Five. I wanted to give the user some feedback of what was going on, so I put a bunch of Console.WriteLine messages in there. That worked OK, but it only gave people feedback when a task was finished, not when a long running task was in progress. What I really wanted was to add a progress update for long running tasks.

Here is an example of what I ended up with;

TunerFree Console Messages

TunerFree Console Messages

The way I went about this was as follows;

1) Multi-Thread the process

Instead of directly calling long running processes, you need to call them in a thread, so I went from code like this;

static void Main(string[] args)

{
...
HuluTranslator hulu = new HuluTranslator();
myList = hulu.loadDatabase(myList, "1");

...
}

to code like this;

using System.Threading;
...
static HuluTranslator hulu = new HuluTranslator();
static void Main(string[] args)

{
...
   Thread huluThread = new Thread(new ParameterizedThreadStart(loadHulu));
   huluThread.Start("");
   while (!hulu.isLoaded)
   {
     Thread.Sleep(1000);
   }
   huluThread.Join();
...
}
static void loadHulu(object o1)
{
   hulu.setPrefs(prefs);
   myList = hulu.loadDatabase(myList, "1");
}

This allowed the code to run in a separate thread, so I can now do work while it is running. Note, at this point it is still a linear process though because we aren’t doing anything but sleeping while the HuluTranslator code runs. Note the use of the isLoaded variable – that’s just a public boolean on the class that I set to true when it has completed the work.

2) Add the overwritten messages

The next key part to the solution is to stop using Console.WriteLine, and to start using Console.Write and Console.Clear. This allows me to present an ever changing message without is scrolling off the screen. Here is the code that I started with;

static void Main(string[] args)

{
...
HuluTranslator hulu = new HuluTranslator();
myList = hulu.loadDatabase(myList, "1");
Console.WriteLine("added " + hulu.added + ", removed " + hulu.removed + ", total "
+ hulu.total + " from " + hulu.pageCount + " web pages");
...
}

adding the multi threading and the new messages takes the code to this;

using System.Threading;
...
static HuluTranslator hulu = new HuluTranslator();
static void Main(string[] args)

{
...
   consoleMessage = "Loading hulu\n";
   Console.Clear();
   Console.Write(consoleMessage);   Thread huluThread = new Thread(new ParameterizedThreadStart(loadHulu));
   huluThread.Start("");
   while (!hulu.isLoaded)
   {
     Console.Clear();
     Console.Write(consoleMessage + "Processing: " + hulu.processed);
     Thread.Sleep(1000);
   }
   consoleMessage += "added " + hulu.added + ", removed " + hulu.removed + ", total "
   + hulu.total + " from " + hulu.pageCount + " web pages\n";
   Console.Clear();
   Console.Write(consoleMessage);
   huluThread.Join();
...
}
static void loadHulu(object o1)
{
   hulu.setPrefs(prefs);
   myList = hulu.loadDatabase(myList, "1");
}

As you can see, by keeping a local copy of the message and constantly clearing it and writing it, we can give feedback every second on how much progress the application has made. This is a much better user experience than just adding new lines to the end all of the time.

Tags:

Leave a Reply