Creating Console application in C#

C

CSharper

I have an application where if the user calls the program with command
line, I want the program to run as a console application and when
called with out parameters it need to launch the windows application.

So I created a windows application and the in the main method I did a
check to see if args has length then not to create the form otherwise
create form. It worked fine. Couple of questions;

1. When I run the program, I get the command prompt back immediatly
instead of it to complete the process. I know this because I create
file and as I press F5 , I can see the file size is getting
increamented. Please help me, how to make this program wait for the
process to complete?

2. Why console.writeline doesn't display the message in the dos box in
WinForm program??

Thanks a lot.
 
G

Göran Andersson

CSharper said:
I have an application where if the user calls the program with command
line, I want the program to run as a console application and when
called with out parameters it need to launch the windows application.

So I created a windows application and the in the main method I did a
check to see if args has length then not to create the form otherwise
create form. It worked fine. Couple of questions;

1. When I run the program, I get the command prompt back immediatly
instead of it to complete the process. I know this because I create
file and as I press F5 , I can see the file size is getting
increamented. Please help me, how to make this program wait for the
process to complete?

2. Why console.writeline doesn't display the message in the dos box in
WinForm program??

Thanks a lot.

That is because you don't have a console application, you just have a
windows application without a form.

In the properties of the project you can set the output type to be a
console application or windows application (or a class library), but you
can't choose this as run time.
 
M

Michael B. Trausch

Change your project type to "console". That will probably fix it.
Albeit with the alternate effect that when run as a GUI application,
you won't get the command prompt back until the application exits.

Keep in mind that, while allowed by the OS, creating windows from a
console application still isn't completely kosher. But it does work
and you wouldn't be the first person to write a program like that. :)

Alright, so I know that I probably oughtn't to chime in here... but a
question for y'all.

Does Windows not support any API wherein a process can:

* start life as a console application
* determine if GUI facilities are wanted/desired
* detach from the console, and
* proceed to live life as a happy GUI program?

Or perhaps:

* start life as a GUI application
* determine if it shouldn't be and _not_ detach from the console,
* proceed to live life as a happy console program?

The only reason I ask is that in something like UNIX, the CLI and GUI
are often both used by software, and applications have the flexibility
to be (or not be, if they don't want to be) GUI applications all at
run-time, so it seems strange to me to not have that kind of ability.

--- Mike
 
G

Göran Andersson

Michael said:
Alright, so I know that I probably oughtn't to chime in here... but a
question for y'all.

Does Windows not support any API wherein a process can:

* start life as a console application
* determine if GUI facilities are wanted/desired
* detach from the console, and
* proceed to live life as a happy GUI program?

No, the console is waiting for the application to end.

You could have a console version and a GUI version of the program, and
the console version could start the GUI version as a new process and exit.
Or perhaps:

* start life as a GUI application
* determine if it shouldn't be and _not_ detach from the console,
* proceed to live life as a happy console program?

No, if you start as a GUI application you don't have any console.
 
C

CSharper

Please post your application's main form load event code.  The problem is
most likely in the startup code.

Mike Ober.- Hide quoted text -

- Show quoted text -

here is the code

public static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main(string[] args)
{
if (args.Count() > 0)
{
Arguments localArgument = new Arguments() { Parameters
= args };
using (StreamWriter writer = new StreamWriter
("log.log"))
{
foreach (string arg in args)
{
writer.WriteLine(arg);
}
}

if (localArgument.IsValidArguments())
{
switch (localArgument.PendingRequest)
{
case Constants.Request.Create:
SourceCreator creator = new SourceCreator
() { ProgramArguments = localArgument };
creator.CreateSourceInformation();
break;
case Constants.Request.Inspect:
Inspector inspector = new Inspector()
{ ProgramArguments = localArgument };
inspector.Inspect();
break;
case Constants.Request.Update:
Updater updater = new Updater()
{ ProgramArguments = localArgument };
updater.Update();
break;
}
}
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

(I know I can do this with interface - the switch part, but I
realised after I completed it)
 
G

Göran Andersson

Michael said:
Groan (sorry - no umlaut) - I have several VB.net applications that do
this. You create a Windows Forms application and then in your form load
event, you determine if you need to show the GUI or create a console
using the Windows API. When you create the console, you can either
create a new thread with the isBackground set to False and exit the
windows form from the load event, or Hide the windows form and then call
the worker code. This is why I asked OP for his form load event code.

Mike.

I see... so what you actually have is a windows application that opens
up a console? If I run the program from a console window, does it open
up a new console? When you run a windows application from the console,
it starts as a separate process and control returns to the console
immediately. The application can't take control over the console again,
can it?
 
C

CSharper

I see... so what you actually have is a windows application that opens
up a console? If I run the program from a console window, does it open
up a new console? When you run a windows application from the console,
it starts as a separate process and control returns to the console
immediately. The application can't take control over the console again,
can it?

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -

Yep I got your point. Here is a thought, In the main thread if I wait
for a particular file to be created in a directory and if the file is
created then get out. Will that work in this case?
 
G

Göran Andersson

CSharper said:
Here is a thought, In the main thread if I wait
for a particular file to be created in a directory and if the file is
created then get out. Will that work in this case?

I'm not sure what you are meaning. Do you want to create a file or check
if a file exists? How does that relate to the original question?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top