Making a Winforms app behave like a Console app

S

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?
 
S

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?
 
S

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.
 
Y

Yan-Hong Huang[MSFT]

Hello Stephen,

Based on my experience, a simple way is to write 3 exes. First is a winform
applicatio, the second is a console application, and the third is a lauch
exe. We can always run the launch.exe and run winform or console exe based
on the switch in launch.exe. It works always and can be understood easily.
The weak point is that you may need to copy some same functions into
winform and console applications.

By the way, Windows Forms apps can have consoles as well. All you have to
do is to create the windows form app and then switch the output type to be
"Console Application" (exe instead of winexe at the command line), and
you'll get both a GUI and a console window.

There are many discussions on it in the group, you may search
http://groups.google.com/groups?ie=UTF-8&q=Winform+console+application+
in IE and can learn from them.

If there are any questions, please feel free to reply here. 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.
 
G

Guest

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.
 

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