Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Console Check

Console Check

Scheduled Pinned Locked Moved C#
csharphtmldatabasecomquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bassam Abdul Baki
    wrote on last edited by
    #1

    I have a console app in C# that runs for a very long time. Basically, it loops through from one to a brazillian for days on end. Depending on conditions met, it will print certain output. Nothing unusual there. Is it possible to press a key and have it tell me which loop index I'm in? I do not want to programmatically print the index every Nth time or second. I would rather have it wait until I press a key and have it spit it out. I guess from a threaded Windows app, this would be easier. I'm trying to see if there's a way to do it from a command-line app on-demand, without having to do it iteratively.

    Web - BM - RSS - Math - LinkedIn

    P T 2 Replies Last reply
    0
    • B Bassam Abdul Baki

      I have a console app in C# that runs for a very long time. Basically, it loops through from one to a brazillian for days on end. Depending on conditions met, it will print certain output. Nothing unusual there. Is it possible to press a key and have it tell me which loop index I'm in? I do not want to programmatically print the index every Nth time or second. I would rather have it wait until I press a key and have it spit it out. I guess from a threaded Windows app, this would be easier. I'm trying to see if there's a way to do it from a command-line app on-demand, without having to do it iteratively.

      Web - BM - RSS - Math - LinkedIn

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You could always use Console.KeyAvailable to do this. This is a none blocking method, so you could use that to output the variable, e.g.

      if (Console.KeyAvailable)
      Console.WriteLine("Currently at index {0}", index);

      I was brought up to respect my elders. I don't respect many people nowadays.
      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      B 1 Reply Last reply
      0
      • B Bassam Abdul Baki

        I have a console app in C# that runs for a very long time. Basically, it loops through from one to a brazillian for days on end. Depending on conditions met, it will print certain output. Nothing unusual there. Is it possible to press a key and have it tell me which loop index I'm in? I do not want to programmatically print the index every Nth time or second. I would rather have it wait until I press a key and have it spit it out. I guess from a threaded Windows app, this would be easier. I'm trying to see if there's a way to do it from a command-line app on-demand, without having to do it iteratively.

        Web - BM - RSS - Math - LinkedIn

        T Offline
        T Offline
        Thomas Daniels
        wrote on last edited by
        #3

        Hi, Try this:

        static int index;
        static void Main(string[] args)
        {
        Console.WriteLine("Press the I key to get loop index");
        System.Threading.Thread thr = new System.Threading.Thread(ReadKey);
        thr.Start();
        for (index = 0; index < 10000; index++)
        {
        // some code
        System.Threading.Thread.Sleep(5); // I added this line to simulate long task
        }
        Console.WriteLine("Press the I key to continue . . .");
        }

        static void ReadKey()
        {
        while (index < 10000)
        {
        ConsoleKeyInfo keyInfo = Console.ReadKey(true);
        if (keyInfo.Key == ConsoleKey.I && index < 10000)
        {
        Console.WriteLine(index);
        }
        }

        }

        Hope this helps.

        The quick red ProgramFOX jumps right over the Lazy<Dog>.

        1 Reply Last reply
        0
        • P Pete OHanlon

          You could always use Console.KeyAvailable to do this. This is a none blocking method, so you could use that to output the variable, e.g.

          if (Console.KeyAvailable)
          Console.WriteLine("Currently at index {0}", index);

          I was brought up to respect my elders. I don't respect many people nowadays.
          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          B Offline
          B Offline
          Bassam Abdul Baki
          wrote on last edited by
          #4

          Perfect, almost! I had to add two more lines arround the WriteLine command:

          Console.Write("\b");

          and

          Console.ReadKey(false);

          The former deletes (backspaces) the character entered, while the latter stops printing a million times. I could also only accept ENTER for printing. :thumbsup:

          Web - BM - RSS - Math - LinkedIn

          M 1 Reply Last reply
          0
          • B Bassam Abdul Baki

            Perfect, almost! I had to add two more lines arround the WriteLine command:

            Console.Write("\b");

            and

            Console.ReadKey(false);

            The former deletes (backspaces) the character entered, while the latter stops printing a million times. I could also only accept ENTER for printing. :thumbsup:

            Web - BM - RSS - Math - LinkedIn

            M Offline
            M Offline
            MicroVirus
            wrote on last edited by
            #5

            Rather than wring a backspace to the console, which I think can be problematic in certain situations, you could use the ReadKey(true) function:

            if (Console.KeyAvailable)
            {
            // Flush the input buffer, suppressing output(echo) of the input
            while (Console.KeyAvailble) Console.ReadKey(true);
            // TODO: Print the index here, for instance something like
            Console.WriteLine("At index {0}", index);
            }

            B 1 Reply Last reply
            0
            • M MicroVirus

              Rather than wring a backspace to the console, which I think can be problematic in certain situations, you could use the ReadKey(true) function:

              if (Console.KeyAvailable)
              {
              // Flush the input buffer, suppressing output(echo) of the input
              while (Console.KeyAvailble) Console.ReadKey(true);
              // TODO: Print the index here, for instance something like
              Console.WriteLine("At index {0}", index);
              }

              B Offline
              B Offline
              Bassam Abdul Baki
              wrote on last edited by
              #6

              Thanks, much better! My method was working without echoing in Win XP, but echoing in Win 7. This way fixes that. Small typo, Console.KeyAvailable.

              Web - BM - RSS - Math - LinkedIn

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups