Connecting Tech Pros Worldwide Forums | Help | Site Map

Command Line Execution

Newbie
 
Join Date: Dec 2008
Posts: 3
#1: Jan 25 '09
I have a program in C#.NET which runs in both GUI and command line mode. When I pass the command line arguments to the executable like:

C:\>myprogram.exe -a file.txt the program is invoked, processes the file and performs a long process. In the meantime the command prompt appears in the command window and it can accept new commands.

I would like to hold the command prompt (prevent it from accepting new commands) till myprogram.exe completes execution.

Suggestions?

vekipeki's Avatar
Expert
 
Join Date: Nov 2007
Posts: 231
#2: Jan 26 '09

re: Command Line Execution


If you change your application's type to Console Application, it will block the prompt when started, but you can still use Windows Forms.

But then a command prompt window will show on every app start, since (I think) you cannot know if the user started you app using a win shortcut or from a command prompt.
Newbie
 
Join Date: Dec 2008
Posts: 3
#3: Jan 26 '09

re: Command Line Execution


Thanks Vek!
The idea is to run the process through multiple batch commands but sequentially. Converting to a console app will address the issue but the extra console screen might not be acceptable when the application is run in GUI mode. Is there anyway to suppress the console screen.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,270
#4: Jan 26 '09

re: Command Line Execution


Subscribing. (Because I have long tried to figure this out as well)
vekipeki's Avatar
Expert
 
Join Date: Nov 2007
Posts: 231
#5: Jan 27 '09

re: Command Line Execution


There are two or three things you could do (that I know of).

You can make a Console app which would serve as a "loader"; if you pass arguments, then it starts a new WinForms process and closes itself, otherwise it does the processing and blocks the prompt until finished.

There are also ways to hide the Console window (check this link for example: Hide Console Window in Console Application In C#.Net « DotNet Friends) once the app is started, but a Console window will appear quickly anyway.

There is also sort of a "hack" that looks like it might work (haven't tested it): How To Create a Console/Window Hybrid Application in C# by Jeffrey Knight. The idea is to create a WinForms app, and then, if you want to block the console, check if the foreground window is actually a Console window and attach to that thread using some Kernel32 calls.

I think these are all you options - if you really want to have a (what he calls a) "hybrid" app.
Newbie
 
Join Date: Dec 2008
Posts: 3
#6: Jan 28 '09

re: Command Line Execution


I tested the "hack". Doesn't work the way I need.
Hiding console is not a smooth solution as it always has some flickering seen in GUI mode till the console window is found and hidden.
I think the best approach is making a "Loader" app. Have not tested it yet how it will behave after invoking the GUI but if it works, the solution will be smooth.
Reply