By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,855 Members | 1,970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,855 IT Pros & Developers. It's quick & easy.

Closing console in Windows Applications

P: n/a
Hello guys!
I can't resolve this problem.. I want my programm in c# working with
only console if there are some parameters, but if someone make
double-click on the exe I want to start the graphic GUI without the
black console on background..
Now I check if there are some parameters and if there aren't parameters
I run the GUI (with the black console that remain..). If I choose
Windows application on Visual Studio, when I run the application from
Console (with parameters) I lose all Console.Write messages... Have you
got a suggestion/trick?!
Thank you to all!
Giojo

Nov 28 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Gijo,

When you create your application create it as a console app, then on your
Main(..) function you can chec if your app does not have any arguments
create the form and start the app.

check the code below:

static void Main(string[] args)
{
if (args.Length > 0)
{
Console.WriteLine("testing App");
Console.ReadLine();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}

this works without any probs on my machine.

Fitim Skenderi
"Giojo" <gi*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hello guys!
I can't resolve this problem.. I want my programm in c# working with
only console if there are some parameters, but if someone make
double-click on the exe I want to start the graphic GUI without the
black console on background..
Now I check if there are some parameters and if there aren't parameters
I run the GUI (with the black console that remain..). If I choose
Windows application on Visual Studio, when I run the application from
Console (with parameters) I lose all Console.Write messages... Have you
got a suggestion/trick?!
Thank you to all!
Giojo

Nov 28 '05 #2

P: n/a
Fitim Skenderi wrote:
When you create your application create it as a console app, then on your
Main(..) function you can chec if your app does not have any arguments
create the form and start the app.

check the code below:

static void Main(string[] args)
{
if (args.Length > 0)
{
Console.WriteLine("testing App");
Console.ReadLine();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}

this works without any probs on my machine.


I've just tested it, and it certainly doesn't solve the OP's problem on
my box:

1) If you run it from the command line, the prompt doesn't return until
the form has been closed.
2) If you double-click on the exe file, you get a console box popping
up, which is exactly what the OP doesn't want.

Jon

Nov 28 '05 #3

P: n/a
You can hide console window but will need PInvoke for this. Just send
relevant message with SendMessage to console window for which you can get
the handle using GetConsoleWindow.
If you want to close console before closing application, you can use Win32
API console allocation / deallocation functions, however this I did not
test - you might want to experiment with this. Check Platform SDK Console
functions sections.

I found so far only one glitch which I can't solve in Net 2.0 - when VS runs
application it leaves orphan cmd.exe after application closes. Problem is
manifesting itself only in VS 2005 environment, not in previous versions.

HTH
Alex

"Giojo" <gi*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hello guys!
I can't resolve this problem.. I want my programm in c# working with
only console if there are some parameters, but if someone make
double-click on the exe I want to start the graphic GUI without the
black console on background..
Now I check if there are some parameters and if there aren't parameters
I run the GUI (with the black console that remain..). If I choose
Windows application on Visual Studio, when I run the application from
Console (with parameters) I lose all Console.Write messages... Have you
got a suggestion/trick?!
Thank you to all!
Giojo

Nov 28 '05 #4

P: n/a
Hi,

Sorry I didn't read the message properly (assumed that console windows is
not a problem :).

One way is to use WinApi to hide the Console window (send a message to the
window to hide itself).

Another way would be to have two apps, one windows and one console. Then
another small console app that would call the appropriate app (start a new
process) by checking the arguments and closing itself.
Fitim Skenderi

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Fitim Skenderi wrote:
When you create your application create it as a console app, then on your
Main(..) function you can chec if your app does not have any arguments
create the form and start the app.

check the code below:

static void Main(string[] args)
{
if (args.Length > 0)
{
Console.WriteLine("testing App");
Console.ReadLine();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}

this works without any probs on my machine.


I've just tested it, and it certainly doesn't solve the OP's problem on
my box:

1) If you run it from the command line, the prompt doesn't return until
the form has been closed.
2) If you double-click on the exe file, you get a console box popping
up, which is exactly what the OP doesn't want.

Jon

Nov 28 '05 #5

P: n/a
>>You can hide console window but will need PInvoke for >>this. Just
send relevant message with SendMessage to >>console window for which you
can get the handle using GetConsoleWindow.

Can you please give some details (code sample) how could this be
achieved ?
Thanks in advance,
Abra

*** Sent via Developersdex http://www.developersdex.com ***
Jan 11 '06 #6

P: n/a
"Abra" wrote:
You can hide console window but will need PInvoke for this. Just
send relevant message with SendMessage to >>console window
for which you can get the handle using GetConsoleWindow.


Can you please give some details (code sample) how could this be
achieved ?


Hello,

I have not tested it but i think you can use the following:

private const Int32 SW_HIDE = 0;

[DllImport("Kernel32.dll")]
private static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

....

IntPtr hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_HIDE);

--
Francois Beaussier


Jan 11 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.