-3

I want to stop the whole do-while loop by using space and write out my WriteLine, but it doesn't do anything on pressing spacebar. I think it's something to do with Thread.Sleep, maybe It doesn't allow user input while on Sleep. I would be really happy if someone could enlighten me why this doesn't work

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

namespace casinov2
{
    class Program
    {
        static void Main(string[] args)
        {
            int x=3;
            int y = 15;
            int[] elemek = new int[7];
            do{
                while (!Console.KeyAvailable){
                    for (int i = 0; i < 5; i++){
                        Console.Clear();
                        Console.SetCursorPosition(x, y);
                        x++;
                        Console.WriteLine("{0:██}{1:██}{2:██}", elemek[i], elemek[i + 1], elemek[i + 2]);
                        System.Threading.Thread.Sleep(100);
                        if (i >= 4){
                            for (int j = 0; j < 5; j++){
                                Console.Clear();
                                Console.SetCursorPosition(x, y);
                                x--;
                                Console.WriteLine("{0:██}{1:██}{2:██}", elemek[i], elemek[i + 1], elemek[i + 2]);
                                System.Threading.Thread.Sleep(100);
                            }
                        i = -1;
                        }
                    }
                }
            } while (Console.ReadKey(true).Key != ConsoleKey.Spacebar);
            Console.WriteLine("ready");
            Console.ReadLine();
        }
    }
}
itelet
  • 36
  • 4
  • `i = -1;` prevents the for-loop from completing. Just delete that. – Hans Passant Nov 29 '19 at 19:56
  • It is not entirely clear what you expected to happen. As Hans points out, by resetting `i`, that loop will never terminate, and so you'll never get to the point where your code tries to read console input. On the other hand, it seems like you really don't want the loop to terminate. If that's the case, then you either need to run the input independently of the output (i.e. in another thread per marked duplicate), or you need to poll inside your loops, checking `Console.KeyAvailable` and breaking out of the loops if that returns `true`. – Peter Duniho Nov 29 '19 at 20:00

1 Answers1

-1

The KeyAvailable check needed to be inside of the for loop:

static void Main(string[] args)
{
    int x=3;
    int y = 15;
    int[] elemek = new int[7];
    do{
        for (int i = 0; i < 5; i++){
            Console.Clear();
            Console.SetCursorPosition(x, y);
            x++;
            Console.WriteLine("{0:██}{1:██}{2:██}", elemek[i], elemek[i + 1], elemek[i + 2]);
            System.Threading.Thread.Sleep(100);
            if (i >= 4){
                for (int j = 0; j < 5; j++){
                    Console.Clear();
                    Console.SetCursorPosition(x, y);
                    x--;
                    Console.WriteLine("{0:██}{1:██}{2:██}", elemek[i], elemek[i + 1], elemek[i + 2]);
                    System.Threading.Thread.Sleep(100);
                }
                i = -1;
            }

            if (Console.KeyAvailable) break;
        }
    } while (Console.ReadKey(true).Key != ConsoleKey.Spacebar);
    Console.WriteLine("ready");
    Console.ReadLine();
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Stuart
  • 5,358
  • 19
  • 28
  • The loops inside the second while are working fine, the question is why doesn't It breaks out of the whole do-while when I press space – itelet Nov 29 '19 at 19:53
  • Because at the end of that block `i` is reset to `-1`. I've tested locally and that has fixed it for me. – Stuart Nov 29 '19 at 19:54
  • 1
    In the loop, the variable `i` will never have a value of `5` nor of any value larger than that. Changing the `if` expression as you suggest would render that block of code entirely moot. Your suggestion that it's an off-by-one-error is definitely wrong. It "fixes" it only by avoiding the code in question altogether. That's not really a fix. (Don't get mislead by the yucky K&R-style bracing and poor indentation...the `i = -1` is inside the inner `for` loop.) – Peter Duniho Nov 29 '19 at 19:54