Home All Groups Group Topic Archive Search About

Making a Winforms app behave like a Console app

Author
31 Jan 2006 9:30 PM
Stephen Walch
I am attempting to add a "console mode" to my Winforms app.  My intention is
that when I start the app with a special switch (for example "myapp.exe
/batch") to does not launch a graphical UI but instead just displays console
messages.

My first guess was to simply change the Main method of the startup class to
do something like this...

if (batchMode)
{
    Console.WriteLine("Now in batch mode");
    ...
}
else
{
    MainForm.Run();   //  Launch graphical user interface
}

Unfortunately, it looks like my app does not behave like a real console mode
app in either case.  When I execute the app from the command line, the app
returns immediately and text output by Console.Write() does not appear
anywhere.

Setting the app as a Console mode application does not give me what I want
either.  With this mode, a big black console output box appears even when in
"GUI mode".

Is there a way to build one executable that supports both modes and
dynamically switches at runtime?

Author
31 Jan 2006 10:08 PM
Stephen Walch
An example of what I am talking about is the .NET ILDASM tool.  If you run
it with the /TEXT option, it stays in console mode.

If you run it with no options, it briefly displays a console window, but
that almost disappears instantly, making it look like a true windows app.
How do they do that?

Show quote
"Stephen Walch" <swalch@online.nospam> wrote in message
news:un3Xg2qJGHA.1728@TK2MSFTNGP09.phx.gbl...
>I am attempting to add a "console mode" to my Winforms app.  My intention
>is that when I start the app with a special switch (for example "myapp.exe
>/batch") to does not launch a graphical UI but instead just displays
>console messages.
>
> My first guess was to simply change the Main method of the startup class
> to do something like this...
>
> if (batchMode)
> {
>    Console.WriteLine("Now in batch mode");
>    ...
> }
> else
> {
>    MainForm.Run();   //  Launch graphical user interface
> }
>
> Unfortunately, it looks like my app does not behave like a real console
> mode app in either case.  When I execute the app from the command line,
> the app returns immediately and text output by Console.Write() does not
> appear anywhere.
>
> Setting the app as a Console mode application does not give me what I want
> either.  With this mode, a big black console output box appears even when
> in "GUI mode".
>
> Is there a way to build one executable that supports both modes and
> dynamically switches at runtime?
>
Author
31 Jan 2006 10:21 PM
Michael D. Ober
It's a console app with Windows Forms added.

Mike.

Show quote
"Stephen Walch" <swalch@online.nospam> wrote in message
news:%23FurWLrJGHA.3936@TK2MSFTNGP12.phx.gbl...
> An example of what I am talking about is the .NET ILDASM tool.  If you run
> it with the /TEXT option, it stays in console mode.
>
> If you run it with no options, it briefly displays a console window, but
> that almost disappears instantly, making it look like a true windows app.
> How do they do that?
>
> "Stephen Walch" <swalch@online.nospam> wrote in message
> news:un3Xg2qJGHA.1728@TK2MSFTNGP09.phx.gbl...
> >I am attempting to add a "console mode" to my Winforms app.  My intention
> >is that when I start the app with a special switch (for example
"myapp.exe
> >/batch") to does not launch a graphical UI but instead just displays
> >console messages.
> >
> > My first guess was to simply change the Main method of the startup class
> > to do something like this...
> >
> > if (batchMode)
> > {
> >    Console.WriteLine("Now in batch mode");
> >    ...
> > }
> > else
> > {
> >    MainForm.Run();   //  Launch graphical user interface
> > }
> >
> > Unfortunately, it looks like my app does not behave like a real console
> > mode app in either case.  When I execute the app from the command line,
> > the app returns immediately and text output by Console.Write() does not
> > appear anywhere.
> >
> > Setting the app as a Console mode application does not give me what I
want
> > either.  With this mode, a big black console output box appears even
when
> > in "GUI mode".
> >
> > Is there a way to build one executable that supports both modes and
> > dynamically switches at runtime?
> >
>
>
Author
31 Jan 2006 10:46 PM
Stephen Walch
Yes, but how is that accomplished?  How is the form launched in such a way
that the the console goes away?

If I just do Application.Run(MyForm), the console Windows remains in the
background.  If I do MyForm.Show() the whole app terminates when I get to
the end of my Main method.

Show quote
"Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in message
news:OcVAvSrJGHA.916@TK2MSFTNGP10.phx.gbl...
>
> It's a console app with Windows Forms added.
>
> Mike.
>
> "Stephen Walch" <swalch@online.nospam> wrote in message
> news:%23FurWLrJGHA.3936@TK2MSFTNGP12.phx.gbl...
>> An example of what I am talking about is the .NET ILDASM tool.  If you
>> run
>> it with the /TEXT option, it stays in console mode.
>>
>> If you run it with no options, it briefly displays a console window, but
>> that almost disappears instantly, making it look like a true windows app.
>> How do they do that?
>>
>> "Stephen Walch" <swalch@online.nospam> wrote in message
>> news:un3Xg2qJGHA.1728@TK2MSFTNGP09.phx.gbl...
>> >I am attempting to add a "console mode" to my Winforms app.  My
>> >intention
>> >is that when I start the app with a special switch (for example
> "myapp.exe
>> >/batch") to does not launch a graphical UI but instead just displays
>> >console messages.
>> >
>> > My first guess was to simply change the Main method of the startup
>> > class
>> > to do something like this...
>> >
>> > if (batchMode)
>> > {
>> >    Console.WriteLine("Now in batch mode");
>> >    ...
>> > }
>> > else
>> > {
>> >    MainForm.Run();   //  Launch graphical user interface
>> > }
>> >
>> > Unfortunately, it looks like my app does not behave like a real console
>> > mode app in either case.  When I execute the app from the command line,
>> > the app returns immediately and text output by Console.Write() does not
>> > appear anywhere.
>> >
>> > Setting the app as a Console mode application does not give me what I
> want
>> > either.  With this mode, a big black console output box appears even
> when
>> > in "GUI mode".
>> >
>> > Is there a way to build one executable that supports both modes and
>> > dynamically switches at runtime?
>> >
>>
>>
>
>
>
Author
1 Feb 2006 2:15 AM
Yan-Hong Huang[MSFT]
Hello Stephen,

I will reply your question in
microsoft.public.dotnet.framework.windowsforms queue. If there is any
question, please feel free to post there and we will follow up.

Thanks very much.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://msdn.microsoft.com/subscriptions/managednewsgroups/

This posting is provided "AS IS" with no warranties, and confers no rights.
Author
1 Feb 2006 7:29 PM
AMercer
Show quote
> I am attempting to add a "console mode" to my Winforms app.  My intention is
> that when I start the app with a special switch (for example "myapp.exe
> /batch") to does not launch a graphical UI but instead just displays console
> messages.
>
> Unfortunately, it looks like my app does not behave like a real console mode
> app in either case.  When I execute the app from the command line, the app
> returns immediately and text output by Console.Write() does not appear
> anywhere.
>
> Setting the app as a Console mode application does not give me what I want
> either.  With this mode, a big black console output box appears even when in
> "GUI mode".
>
> Is there a way to build one executable that supports both modes and
> dynamically switches at runtime?

Lets say your app's name is app (you run app.exe), and that it is a windows
program and not a console program.  If you open a command prompt window and
type in
   app
from the appropriate path, your console writes will go to stdout and from
there to the bit bucket because (as I understand it) MS has redirected stdout
to the bit bucket as part of running your winform program.  Instead, try
   app | more
from the command line, and you will see your console writes.

So far, so good.  But how can we be rid of "| more"?  In other words, I
would like my writes to stdout to go to the obvious place, namely the bit
bucket if launched from by clicking an icon, or the command window if
launched from a command window.  How to do this?  I don't know.

In my app, I process some command line parameters, and in some cases, I
write to the console (errors, command line usage instructions, etc). 
Regardless of how I launch the program, no problems ensue from these writes. 
If I want to see what I write to stdout via console writes, I start a command
line and run piped to more as indicated above.  Just in case, my apps include
a command line parameter to disable console writes, but I have never had to
use it.  FYI all experiences are with FW 1.1 and Windows XP 5.1, SPs up to
date.

I asked your question a while back, got nowhere.  I don't like the three exe
solution at all.  I'll monitor this posting with interest, but I doubt we
will get anywhere.  Regardless,  if you can stand piping the stdout to more
or a file, that gets you a workaround.

AddThis Social Bookmark Button