Run WinForms app as Console app

J

Joe

We pass args to our WinForms app and would like the console to wait until
the program ends before returning.

For example:
C:> MyApp.exe -r something

This returns even though the process is still running (Task manager,
processes).

I would like it to wait until the process is done.

Any ideas?

-Joe
 
P

Peter Duniho

We pass args to our WinForms app and would like the console to wait until
the program ends before returning.
[...]
I would like it to wait until the process is done.

Any ideas?

Compile the application as a console application. You'll still be able to
create forms, but it should act more like a regular console application
otherwise.

Alternatively, make a stub console application that creates your main
application as a new process and then waits for it.

Pete
 
W

Walter Wang [MSFT]

Hi Joe,

This has nothing to do with the WinForm application. To wait before a GUI
application returns in command prompt, use "/wait" switch of start command:

C:> start /wait notepad.exe


Please note if the path to your application has space, you need to use
following command line:

C:> start "foo title" /wait "c:\program files\myapp\myapp.exe" -r something


Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Joe

Hi Peter,

I'm going to use your suggestion and create a console app to launch my app.
This will allow the Console.Write to display also.

Thanks,
Joe
Peter Duniho said:
We pass args to our WinForms app and would like the console to wait until
the program ends before returning.
[...]
I would like it to wait until the process is done.

Any ideas?

Compile the application as a console application. You'll still be able to
create forms, but it should act more like a regular console application
otherwise.

Alternatively, make a stub console application that creates your main
application as a new process and then waits for it.

Pete
 
J

Joe

I created a simple console app to run my WinForms application but nothing
displays in the console.

proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.Start();
proc.WaitForExit();

If I run my WinForms app MyApp.exe -args >> logfile.txt
the file is populated.

Am I doing something wrong?

Joe said:
Hi Peter,

I'm going to use your suggestion and create a console app to launch my
app. This will allow the Console.Write to display also.

Thanks,
Joe
Peter Duniho said:
We pass args to our WinForms app and would like the console to wait
until
the program ends before returning.
[...]
I would like it to wait until the process is done.

Any ideas?

Compile the application as a console application. You'll still be able
to create forms, but it should act more like a regular console
application otherwise.

Alternatively, make a stub console application that creates your main
application as a new process and then waits for it.

Pete
 
J

Joe

I got it. I made the following changes:

proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.OutputDataReceived += new
DataReceivedEventHandler(proc_OutputDataReceived);
proc.Start();
proc.BeginOutputReadLine();
proc.WaitForExit();

-Joe

Joe said:
I created a simple console app to run my WinForms application but nothing
displays in the console.

proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.Start();
proc.WaitForExit();

If I run my WinForms app MyApp.exe -args >> logfile.txt
the file is populated.

Am I doing something wrong?

Joe said:
Hi Peter,

I'm going to use your suggestion and create a console app to launch my
app. This will allow the Console.Write to display also.

Thanks,
Joe
Peter Duniho said:
We pass args to our WinForms app and would like the console to wait
until
the program ends before returning.
[...]
I would like it to wait until the process is done.

Any ideas?

Compile the application as a console application. You'll still be able
to create forms, but it should act more like a regular console
application otherwise.

Alternatively, make a stub console application that creates your main
application as a new process and then waits for it.

Pete
 
P

Peter Duniho

I created a simple console app to run my WinForms application but nothing
displays in the console.

I see from your other post that you managed to get this block of code to
do something akin to what you wanted. Whether it's exactly what you
wanted, I don't know. You shouldn't be setting the "RedirectStandard..."
properties unless you really want to get access to those streams
internally. If that's really what you want to do, then adding code to
actually get the redirected output is the right fix. However, if you only
added that code just to get the thing to work, then the right fix is to
stop setting the "RedirectStandard..." properties to true.

And of course, I neglected to mention in my first reply that if it's
suitable to use the "start" command with the "/wait" switch, that's an
even easier mechanism to do what you want. Fortunately, someone else did
bring that up.

Pete
 

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