.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

C# Messages to console getting lost

I'm normally a Java developer, but yesterday was tasked with my first ever C# project.  Wouldn't you know, its due today.  It revolves around some code generated using XMLSpy.  For whatever reason calls to Console.Writeline() don't show up in the output, but calls to Console.Beep() successfully produce an audible beep.  Below is the code and output.  Thanks for any help:

namespace C2rChanges
{
    /// <summary>
    /// Summary description for C2rChangesTest.
    /// </summary>
    class C2rChangesTest
    {
        protected static void Example()
        {
            //
            // TODO:
            //  Insert your code here...
            //
            // Example code to create and save a structure:
            //  C2rChangesDoc doc = new C2rChangesDoc();
            //  C2rUnitChangeType root = new C2rUnitChangeType();
            //  ...
            //  doc.SetRootElementName("http://peoc3t.us.army.mil/abcs", "C2rUnitChange");
            //  doc.SetSchemaLocation("C2rChanges.xsd"); // optional
            //  doc.Save("C2rChanges1.xml", root);
            //
            // Example code to load and save a structure:
            Console.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            Console.Beep();
              C2rChangesDoc doc = new C2rChangesDoc();
              C2rChangesType root = new C2rChangesType(doc.Load("C:\\src\\dci\\CSharp\\C2rChanges\\C2rChanges.xml"));
            //  ...
            //  doc.Save("C2rChanges1.xml", root);
            //
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        public static int Main(string[] args)
        {
            try
            {
                Console.WriteLine("C2rChanges Test Application");
                Example();
                Console.WriteLine("OK");
                return 5;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return 1;
            }
        }
    }
}

And the output

'C2rChangesTest.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'C2rChangesTest.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'C2rChangesTest.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'C2rChangesTest.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'C2rChangesTest.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'C2rChangesTest.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'C2rChangesTest.vshost.exe' (Managed): Loaded 'C:\src\dci\Csharp\C2rChangesTest\bin\Debug\C2rChangesTest.vshost.exe', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'C2rChangesTest.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'C2rChangesTest.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread '<No Name>' (0x174) has exited with code 0 (0x0).
'C2rChangesTest.vshost.exe' (Managed): Loaded 'C:\src\dci\Csharp\C2rChangesTest\bin\Debug\C2rChangesTest.exe', Symbols loaded.
'C2rChangesTest.vshost.exe' (Managed): Loaded 'C:\src\dci\Csharp\C2rChangesTest\bin\Debug\C2rChanges.dll', Symbols loaded.
'C2rChangesTest.vshost.exe' (Managed): Loaded 'C:\src\dci\Csharp\C2rChangesTest\bin\Debug\Altova.XML.dll', Symbols loaded.
The thread 0xcfc has exited with code 0 (0x0).
The thread '<No Name>' (0xe20) has exited with code 0 (0x0).
The program '[3680] C2rChangesTest.vshost.exe: Managed' has exited with code 0 (0x0).
Ken Klose Send private email
Thursday, January 25, 2007
 
 
It looks to me like you are looking at the output window not a console. If you want to use the output window, try changing all the Console.xxx to Trace.xxx. You will have to add a using statement for the System.Diagonostics namespace.
Roger Jack Send private email
Thursday, January 25, 2007
 
 
OK, here's a naive question - the console window and the output window are two different windows?

How do I view the console window?
Ken Klose Send private email
Thursday, January 25, 2007
 
 
The console window is not in the IDE itself. Look for another window on your taskbar that is a standard windows console window. It will have the output in it.
dood mcdoogle
Thursday, January 25, 2007
 
 
You mean like a DOS command line window?  When I run, one pops open, but just as quickly closes (because the program terminates).  Is there anyway to keep this guy around?

I'm using Visual C# 2005 Express Edition.

Thanks,
Ken
Ken Klose Send private email
Thursday, January 25, 2007
 
 
console.readline()
rbny
Thursday, January 25, 2007
 
 
That's a clever hack.  Unfortunately it only works some of the time.  If there is an exception (for instance I change the absolute path of the file I'm loading to relative - and wrong) then no dice, the window still closes without waiting for me to press a key.
Ken Klose Send private email
Thursday, January 25, 2007
 
 
Looks like you're trying to run your project as a windowed application.  You must change the project type to console application -- there should be a switch among the compiler options.
Chris Nahr Send private email
Thursday, January 25, 2007
 
 
Yeah...I was going to say the same thing.  I believe there is a compiler option to allow or supress console output.
grover
Thursday, January 25, 2007
 
 
Chris,

I set it up initially as a console app, and I just checked the properties and its still listed as a console app, so that's not it.

Thanks,
Ken
Ken Klose Send private email
Thursday, January 25, 2007
 
 
You must be running .NET 2.0. In .NET 1.x the console window would not close without you specifically pressing a key. It was annoying but also nice for the reasons you give. In .NET 2.0 they took away this "feature" and now the window closes automatically when the program ends. I don't know if there is a setting somewhere to change this behavior but either way you will probably want to set a breakpoint somewhere in your code before the application exits. As for the possibility of an exception, you should be using a globabl exception handler anyway so that would give you a place to catch the exit point.

Also, as others have stated, if you are just trying to show debug output you can use the Trace object in the System.Diagnostics namespace instead of the Console object. The trace then goes to the Output window instead of the console.

Having used several Java IDE's I've always found this aspect of Vistual Studio pretty annoying. Having a console window within the IDE always seemed better because the cut/paste capabilities weren't as limited as what you get with a standard Windows console window. But luckily, I don't have to write many .NET console apps.

Good luck.
dood mcdoogle
Thursday, January 25, 2007
 
 
A console application is designed to be run from ....... (drum roll please)..... a console window.  If you run it by itself, it will create a temporary console window and destroy it when it is done.  The real-life use of a console application is either to be run interactively from an existing console window or for stdout, stderr, and stdin to be redirected to/from something else.

Not being able to see the output of a console application underscores a basic non-understanding of what it is for.  Before you go and build a console application, perhaps you should run a few to see how they are supposed to work. 

Try this to see the exact same situation with a well-tested, mature application.  ipconfig.exe is a console application.  Try Start...Run...iconfig.  It will do exactly what your app is doing because that is how they are supposed to work.  Now try Start...run...cmd to open a new console and then ipconfig in the existing console.  It works much better this way.

One way to make the Visual Studio runtime environment simulate the real world conditions of a console application is to set the project properties to launch a seperate console.  Set the start options to "Start External Program" and choose C:\Windows\System32\cmd.exe as the external program.  Then set the command line arguments to "/k path_to_app\name_of_app.exe".  This will launch your app in its own console when it runs, and leave that window open when the application exits.  Unfortunately, the debugger won't automatically attach on startup.

BTW, it isn't a DOS window.  DOS died years ago.  The Windows XP cmd.exe program was actually written from scratch to be backwards compatible with the DOS command line syntax, but it doesn't include any DOS code.  It does however, support a lot of non-DOS command line syntax.  Stuff like:

route print 2>&1 | more

has worked in the NT console for 15 years, but was never a feature of DOS.
JSmith Send private email
Friday, January 26, 2007
 
 
"Not being able to see the output of a console application underscores a basic non-understanding of what it is for.  "

I think that is a little harsh. Coming from the Java side I'm surprised that VS doesn't capture the standard IO streams and pipe it to a window internal to the IDE. All Java IDE's do this for console apps.

And considering that Microsoft changed the behavior of debugging console apps in VS 2005 I'd say that there is some room for confusion. In VS 2003 the console window would stay open until you explicitly hit a key. If this behavior had been maintained the OP probably wouldn't have had to ask the question at all. So the change in VS 2005 makes debugging console applications much closer to "real life" conditions. But we all know that in this case the real world is much less than ideal.
dood mcdoogle
Friday, January 26, 2007
 
 
If you're running from the IDE, put a debugger breakpoint on the last line (just before the application exits).
Christopher Wells Send private email
Friday, January 26, 2007
 
 
press ctrl+f5 instead of f5
Husker Send private email
Wednesday, January 31, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz