views:

67

answers:

3

When building a Windows Console App in C#, is it possible to update lines in the console while waiting for a readline?

My current code is:

do
{
    Console.Clear();
    Console.WriteLine("RA:     " + scope.RightAscension);
    Console.WriteLine("Dec:    " + scope.Declination);
    Console.WriteLine("Status: " + scope.Slewing);
    System.Threading.Thread.Sleep(1000);
} while (true);
+1  A: 

Use Console.KeyAvailable inside the loop. As soon as it returns true, the user started typing so call ReadLine(). It doesn't make for a very attractive user interface though. Consider Windows Forms.

Hans Passant
+1  A: 

Yes. You can write to the Console from a separate thread while blocking on Console.ReadLine.

That being said, it's going to cause confusion. In your case, you'll clear out what the user is typing half-way through their line (via Console.Clear()), plus move the cursor position around dramatically.


Edit: Here's an example that shows this:

namespace ConsoleApplication1
{
    using System;
    using System.Threading;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting");

            ThreadPool.QueueUserWorkItem(
                cb =>
                    {
                        int i = 1;
                        while (true)
                        {
                            Console.WriteLine("Background {0}", i++);
                            Thread.Sleep(1000);
                        }
                    });
            Console.WriteLine("Blocking");
            Console.WriteLine("Press Enter to exit...");
            Console.ReadLine();
        }
    }
}

If you run this, you'll see the Console waits on ReadLine, but the background thread still prints.

Reed Copsey
Can you give a code example? I am rather new to C#. I wondered if threads were the answer.
Arlen Beiler
@Arlen: Done. It's there.
Reed Copsey
What are your using statements?
Arlen Beiler
@Arlen: I changed it to .NET 2, and included a fully compilable program for you...
Reed Copsey
I accepted this answer because it gives the most detail. The reason he has almost 100K is because he gives excellent answers such as these, not vice versa. So don't worry, you can do it too.
Arlen Beiler
What does `cb` in `cb =>` stand for?
Arlen Beiler
@Arlen: It's for hte lambda. QueueUserWorkItem uses a WaitCallback delegate, that's passing state as a parameter. I'm just using "cb" for the name of the state object (that I ignore completely)
Reed Copsey
@Arlen: It can be any name you choose - (Often, for lambdas that require an argument, where the argument will be unused, you'll see "_ => " used since _ is a valid identifier)
Reed Copsey
A: 

May this solution helps you:

using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace WaitForExit
{
    class Program
    {
        static void Main(string[] args)
        {
            new Thread(() =>
                {
                    Console.ReadLine();
                    Environment.Exit(0);
                }).Start();

            int i = 0;
            while (true)
            {
                Console.Clear();
                Console.WriteLine(++i);
                Thread.Sleep(1000);
            }
        }
    }
}    
youllknow