GregH
GregH

Reputation: 12888

Beeping in a vb.net console application until a press the spacebar

How can I continue processing in a vb.net console application until I press a key? I basically want to loop playing a beep every half second until the user presses any key. I have the loop and beeping down:

Do Until <key is pressed>
   Console.Beep()
   Sleep(500)
Loop

The question is, how do I implement the "<key is pressed>" condition?

Upvotes: 0

Views: 1545

Answers (2)

KingOfAllTrades
KingOfAllTrades

Reputation: 421

I know this an old question, but I just had to do this and found what I consider a much simpler way to loop until a key is pressed.

While Not Console.KeyAvailable
'do stuff
Loop

It is working well under .NET 4.0 on Win 7. Once a key is pressed, the loop is exited.

Upvotes: 0

Niranjan Singh
Niranjan Singh

Reputation: 18260

Look at this simple example to create a simple program for making a typewriter like stuff.

int i;
char c;
while (true)
{
 i = Console.Read ();
 if (i == -1) break;
 c = (char) i;
 Console.WriteLine ("Echo: {0}", c);
}
Console.WriteLine ("Done");
return 0;

you can make a infinite loop as all beginners do when they create some little console games in C. just make a exit statement that let your program to break the flow. simply a Escape key pressed

you can follow the Tariqulazam comment link that may help you implement in .net.

Console.WriteLine("Press any key to stop");
do {
    while (! Console.KeyAvailable) {
        // Do something
   }       
} while (Console.ReadKey(true).Key != ConsoleKey.Escape);

try to use this method Beep(int, int). follow on this link.

http://msdn.microsoft.com/en-us/library/4fe3hdb1.aspx

The Beep method is not supported on the 64-bit editions of Windows Vista and Windows XP.

Upvotes: 1

Related Questions