473,382 Members | 1,386 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,382 software developers and data experts.

Closing console in Windows Applications

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
6 10401
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
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
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
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
>>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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
7
by: Miktor | last post by:
At the minute I am using the following rather unwieldy function to prevent the console window in Dev C++ from closing before I can see the output from my program: // function to prevent the...
3
by: Dave | last post by:
I created a console app in Visual Studio 2003 using C#. How to I distribute this app after I build it? It is my understanding that a C# app will run on any machine with the Net Framework...
5
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough...
3
by: Andrew | last post by:
I get a Null Reference Exception if I close a non-modal dialog (that is, a form opened with Show()) when a selection is made from a ComboBox. The error message refers to Unsafe Native Methods, but...
1
by: Vegar Hansen | last post by:
Hello everone. Can somebody help me? When I run my code I can read, write to and delete from database, but when I have done one of these things and try to copy the databasefile from my folder...
2
by: wayne calvert | last post by:
hello all just started learning vb.net and i'm having problem with console applications. according to the book i using when i put out the old 'Hello from Visual Basic' thing i should be able to...
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...
11
by: Simon van Beek | last post by:
Dear reader, By opening an application I get always the main Access window with the closing cross in the above right corner. Is there a possibility to make this closing cross invisible? ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.