473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 3211
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mullin Yu | last post by:
But, I want is that I can have a Main app that will start a new process or kill one particular or all process. The process will open a console exe. But, I don't want the user to close the console...
1
by: James Carnley | last post by:
I am learning how to use windows forms and I have come across a small problem. Whenever I run my program it opens a console window before launching the windows form. The console doesnt go away...
12
by: Jarod_24 | last post by:
I got a project called "Forms" that hold some forms and stuff in my solution. A argument at startup defines wether to use a From or Console. My plan was to create a myConsole class that would...
8
by: Alison | last post by:
Hi, Al I am trying to design a user interface which provides both menus and toolbars for some users to click on whatever they want to do, at the same time, I would like to have a console window...
3
by: Dean Slindee | last post by:
I have a exception handling class that could be called from either a windows project app or a console project app. Is there any way for this class to determine which type of app called it without...
2
by: Chris Mullins | last post by:
For some reason the question, "Can I make an EXE that is both a Windows Service and a Console Application?" has come up quite a bit for me over the last few weeks. I've been doing this for...
4
by: Mo | last post by:
Hi, I am writing a console application to send a key sequence to an old clunky application on a regular interval using the windows scheduler. I can get it to work if it is a windows form...
4
by: Peter Nimmo | last post by:
Hi, I am writting a windows application that I want to be able to act as if it where a Console application in certain circumstances, such as error logging. Whilst I have nearly got it, it...
12
by: Dilip | last post by:
Hi All I have a server based C# console application. This application must hide its console window when its launched out on the field. So I dutifully P/Invoke'd FindWindow/ShowWindow...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.