Console Windows Forms hybrid

M

Mark Allison

Hi,

I have an application that I want to be to run in Console mode and GUI
mode. If no params are entered, I want the GUI fired up, if params are
entered, then go into console mode.

I believe I have all the code set up to do this, however when I issue a
Console.WriteLine instruction, nothing gets written to the console. In
fact, the command prompt comes back before the program has finished
executing. What do I need to do?

Thanks.
--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk

Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html
 
M

Morten Wennevik

Hi Mark,

Could you show us your Main method?
Are you compiling as Windows or Console application? I believe you need to build it as a Console Application to make it work
 
M

Mark Allison

OK, I found out where - I did this to get it to work. In Visual Studio,
click Project, Properties; in the Common Properties section, General,
Output Type is Console Application.

I now have another issue. If I launch the application from Windows
Explorer, a blank console window gets launched with it. How do I prevent
that?

It works fine if launched from a console. Thanks for your help.
--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk

Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html


Mark said:
Thanks Morten, here's my Main method:

[STAThread]
public static void Main(string[] args)
{
if (args.Length == 0)
{
Application.Run(new frmFindDBWin());
}
else
{
Console.WriteLine ("Nothing happens here");
}
}

How do I compile it as a Console Application?


--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk

Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html


Morten said:
Hi Mark,

Could you show us your Main method?
Are you compiling as Windows or Console application? I believe you
need to build it as a Console Application to make it work
 
M

Morten Wennevik

OK, I found out where - I did this to get it to work. In Visual Studio,
click Project, Properties; in the Common Properties section, General,
Output Type is Console Application.

I now have another issue. If I launch the application from Windows
Explorer, a blank console window gets launched with it. How do I prevent
that?

Good question, but I don't know the answer to that.
Alternately you might try a windows application and if console mode, hide the window, create a command prompt and route console text to that using StandardOutput, although I'm not sure how to do this either.
 
I

Ian Griffiths [C# MVP]

The standard solution to this is to have two executables. This is exactly
what Visual Studio .NET does.

The technique relies on two facts:

(1) the command line always prefers .COM executables to .EXEs
(2) a .COM executable is allowed to be a proper PE executable (it doesn't
have to be an old-style DOS .COM app)

So you make the .COM the console application, and the .EXE the Windows
application. In the console application, if no parameters are passed on the
command line, just launch the EXE and then exit.

This is why when you just type "devenv" at the command prompt VS.NET
launches in the usual way (assuming VS.NET is on your path), but if you pass
in parameters, it runs as a console app.

Of course you probably don't want to ship two copies of your application for
this purpose...

In practice what you usually want to do is have one real copy of the
application. This is workable - I have a little shim launcher program whose
sole purpose is to allow what you're trying to do here - to allow a windows
app to run normally when run without parameters, but to run as a console app
when parameters are passed. You can use the same shim application for
everything - all you need to do is compile it and then rename. The source
code is here:

http://www.interact-sw.co.uk/utilities/winappconsole/source/

Compile this into a file called "MyApplication.com". (Or whatever you want
to call it - so long as it ends in .com.) Then copy the real EXE into the
same directory. (You must build the EXE as a Windows Application, not a
Console application.)

The nice thing about this launcher program is that you don't really need to
do anything special in the main exe. You will be able to access the command
line switches in the normal way from either Environment.CommandLine, or via
the 'args' parameters passed to main. You don't really need to anything
special at all. If you run it as

MyApplication

from the command line, it'll just run as a normal windows app, without
attaching to the console. If you run it as:

MyApplication /some /parameters

then it will run as a console application.

And as for running the program from explorer, just double click on the EXE.

Hope that helps.


--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

Mark Allison said:
OK, I found out where - I did this to get it to work. In Visual Studio,
click Project, Properties; in the Common Properties section, General,
Output Type is Console Application.

I now have another issue. If I launch the application from Windows
Explorer, a blank console window gets launched with it. How do I prevent
that?

It works fine if launched from a console. Thanks for your help.

Mark said:
Thanks Morten, here's my Main method:

[STAThread]
public static void Main(string[] args)
{
if (args.Length == 0)
{
Application.Run(new frmFindDBWin());
}
else
{
Console.WriteLine ("Nothing happens here");
}
}

How do I compile it as a Console Application?
Morten said:
Hi Mark,

Could you show us your Main method?
Are you compiling as Windows or Console application? I believe you need
to build it as a Console Application to make it work
 

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