473,703 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne 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 3234
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.WriteLi ne ("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.WriteLi ne ("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.tinne d.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.Com mandLine, 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.WriteLi ne ("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.Com mandLine, 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
2782
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 windows by themselves, and force them to use the Main app. If I use Windows Services, I'm not sure can I create one dynamically, and then kill easily. But, if using console application, my following coding is working now. using System;
1
2580
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 until the program exits. When you use VS.net to make a windows application it makes a form without a console and I dont know why. What specifies whether a program starts with a console or not? I know that this code is probobly not right but I...
12
3751
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 also allow my application to display a Console, but i don't seem to be allowed to do this. I don't see any References that i need to add, so how do i do this.
8
18651
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 available in the same form for users to enter commands and display outputs if some prefer to use character based user interface. I would like to implement the user interface in vb .net. Now I have menus and toolbars ready, but do not now how to get a...
3
1966
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 sending an window/console parameter from either app? Thanks, Dean Slindee
2
2625
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 years, but for some reason I've never seen it documented anywhere. C# (or VB.Net) is perfectly happy to create an EXE that can be: 1 - Both a WinForms App or a Windows Service 2 - Both a Console App or a Windows Service
4
8009
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 application but not in a console application. Two questions I have: 1) how can I get this code modified to use System.Windows.Forms.Keys 2) I like to pass a parameter to the console app to indicate which key needs to be passed to the application. so:
4
10510
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 doesn't seem to write to the screen in the way I would expect. The output is:
12
6537
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 combination to hide the console window at launch time. The application (for legacy reasons) hangs around by waiting on an old- fashioned Console.ReadLine() statement.
0
8658
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9239
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9096
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6583
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5919
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4674
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2419
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2049
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.