472,110 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

Console Windows Forms hybrid

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
Nov 16 '05 #1
6 3117
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

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
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 Wennevik wrote:
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

Nov 16 '05 #3
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 Allison wrote:
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 Wennevik wrote:
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

Nov 16 '05 #4
On Fri, 24 Sep 2004 09:58:28 +0100, Mark Allison <ma***@no.tinned.meat.mvps.org> wrote:
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5
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/utiliti...onsole/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" wrote:
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 wrote:
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 Wennevik wrote:
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

Nov 16 '05 #6
Thanks for your help Ian, that works great, but it still seems messy to
me. I don't want to have to ship an extra file just to get around this.
It seems like a workaround to me, not an elegant solution.

Is this the only way?

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

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

Ian Griffiths [C# MVP] wrote:
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/utiliti...onsole/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.

Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by James Carnley | last post: by
12 posts views Thread by Jarod_24 | last post: by
2 posts views Thread by Chris Mullins | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.