Console Applications

G

Guest

I'm a complete newbie at programming. I chose to learn C# as my programming language. Every book I purchase starts with console applications. Each time I try to run the apps that I build, it will only display for a few seconds then it will disappear? I'm running Windows XP and have VS.net. Is there some way that I can view the output of my console applications with out it shutting off on me? I'm getting discouraged because I want to build Windows apps and not console apps right now? I can't seem to run any command line apps. I'm stuck because these books teach you the basics of the language using console apps. I want to skip past this but I miss out on so much. Please help!!!!
 
J

Joe Delekto

Greets,

When you run your console application, use Ctrl-F5 instead of F5 to
launch. This will make sure that your console application waits for you to
close it after it has completed. If any exceptions are thrown, you can then
hook to the VS.NET debugger and step through your code.

If you do want to step through and debug it, just use F9 on the first
line in your console application and F10 to step over or F11 to step into
the application. As you debug, you can click on the application in your
taskbar to view the current output that is being displayed in the console.

Regards,

Joe

Michael W said:
I'm a complete newbie at programming. I chose to learn C# as my
programming language. Every book I purchase starts with console
applications. Each time I try to run the apps that I build, it will only
display for a few seconds then it will disappear? I'm running Windows XP
and have VS.net. Is there some way that I can view the output of my console
applications with out it shutting off on me? I'm getting discouraged
because I want to build Windows apps and not console apps right now? I
can't seem to run any command line apps. I'm stuck because these books
teach you the basics of the language using console apps. I want to skip
past this but I miss out on so much. Please help!!!!
 
A

Andreas Håkansson

Michael,

First of all let me welcome you to the wonderfull world of .NET and to
congratulate you on your excellent choise of programming langauge ;) To
get back to your problem; the short answer to your question is that your
application runs out of code to execute and there for terminates. If you
were to add a loop such as a while-loop then the application would stay
in that loop thus not running out of code to execute.

The easy, and most common way, to prevent the application to terminate
before you want to (so you perhaps can read the output it created) you
could add Console.ReadLine() at the end of your Main method. This tells
the console application to wait for you to input something, and not continue
until you have done so. This makes your console application pause just
before it is about to terminate

Try the following:

[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadLine();
}

And you should see that your application does not end without you pressing
enter. The reason most books does not include this is because they expect
you to execute it from the .NET Command Prompt (like a DOS prompt but
with .NET environment variables set) where the window still stays open when
the application has finished executing.

Hope this helps,

//Andreas

Michael W said:
I'm a complete newbie at programming. I chose to learn C# as my
programming language. Every book I purchase starts with console
applications. Each time I try to run the apps that I build, it will only
display for a few seconds then it will disappear? I'm running Windows XP
and have VS.net. Is there some way that I can view the output of my console
applications with out it shutting off on me? I'm getting discouraged
because I want to build Windows apps and not console apps right now? I
can't seem to run any command line apps. I'm stuck because these books
teach you the basics of the language using console apps. I want to skip
past this but I miss out on so much. Please help!!!!
 
H

Hayato Iriumi

Yeah, you can add...

Console.ReadLine()

at the end of your code. Or you open command line first and then execute
your executable from command line instead of double-clicking it.


Michael W said:
I'm a complete newbie at programming. I chose to learn C# as my
programming language. Every book I purchase starts with console
applications. Each time I try to run the apps that I build, it will only
display for a few seconds then it will disappear? I'm running Windows XP
and have VS.net. Is there some way that I can view the output of my console
applications with out it shutting off on me? I'm getting discouraged
because I want to build Windows apps and not console apps right now? I
can't seem to run any command line apps. I'm stuck because these books
teach you the basics of the language using console apps. I want to skip
past this but I miss out on so much. Please help!!!!
 
I

Ismail Rajput

put this code after your code

Console.read();


Michael W said:
I'm a complete newbie at programming. I chose to learn C# as my
programming language. Every book I purchase starts with console
applications. Each time I try to run the apps that I build, it will only
display for a few seconds then it will disappear? I'm running Windows XP
and have VS.net. Is there some way that I can view the output of my console
applications with out it shutting off on me? I'm getting discouraged
because I want to build Windows apps and not console apps right now? I
can't seem to run any command line apps. I'm stuck because these books
teach you the basics of the language using console apps. I want to skip
past this but I miss out on so much. Please help!!!!
 
M

Michael A. Covington

It disappears because it's finished running. Add something like this:

Console.Write("Press Enter...");
Console.ReadLine();

near the end of your program.

For fancier tricks with the console, see also www.ai.uga.edu/mc/Konsole.zip.



Michael W said:
I'm a complete newbie at programming. I chose to learn C# as my
programming language. Every book I purchase starts with console
applications. Each time I try to run the apps that I build, it will only
display for a few seconds then it will disappear? I'm running Windows XP
and have VS.net. Is there some way that I can view the output of my console
applications with out it shutting off on me? I'm getting discouraged
because I want to build Windows apps and not console apps right now? I
can't seem to run any command line apps. I'm stuck because these books
teach you the basics of the language using console apps. I want to skip
past this but I miss out on so much. Please help!!!!
 
W

William Stacey [MVP]

If you write something like Console.Writeline("hello"); You need to add a
Console.Read() as last line so the Console windows does not write hello and
close (it closes automatically in debug mode after it runs.) The
Console.Read or Console.Readline will wait for user input (i.e. enter)
before closing so you can see what you output to console. HTH

--
William Stacey, MS MVP


Michael W said:
I'm a complete newbie at programming. I chose to learn C# as my
programming language. Every book I purchase starts with console
applications. Each time I try to run the apps that I build, it will only
display for a few seconds then it will disappear? I'm running Windows XP
and have VS.net. Is there some way that I can view the output of my console
applications with out it shutting off on me? I'm getting discouraged
because I want to build Windows apps and not console apps right now? I
can't seem to run any command line apps. I'm stuck because these books
teach you the basics of the language using console apps. I want to skip
past this but I miss out on so much. Please help!!!!
 
M

Mike Kitchen

Hi, I have an example on my web site that explains this

http://www.publicjoe.f9.co.uk/csharp/csharp04.html

Hope this helps

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html


Michael W said:
I'm a complete newbie at programming. I chose to learn C# as my
programming language. Every book I purchase starts with console
applications. Each time I try to run the apps that I build, it will only
display for a few seconds then it will disappear? I'm running Windows XP
and have VS.net. Is there some way that I can view the output of my console
applications with out it shutting off on me? I'm getting discouraged
because I want to build Windows apps and not console apps right now? I
can't seem to run any command line apps. I'm stuck because these books
teach you the basics of the language using console apps. I want to skip
past this but I miss out on so much. Please help!!!!
 
G

Guest

I would like to thank all of you for the help. Your advice well help me stay with it and not be discouraged.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top