Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

Run WinForms app as Console app

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe - 21 May 2007 18:08 GMT
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
Peter Duniho - 21 May 2007 18:50 GMT
> 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
Joe - 22 May 2007 14:32 GMT
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

>> We pass args to our WinForms app and would like the console to wait until
>> the program ends before returning.
[quoted text clipped - 11 lines]
>
> Pete
Joe - 22 May 2007 15:06 GMT
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?

> Hi Peter,
>
[quoted text clipped - 20 lines]
>>
>> Pete
Joe - 22 May 2007 15:43 GMT
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

>I created a simple console app to run my WinForms application but nothing
>displays in the console.
[quoted text clipped - 34 lines]
>>>
>>> Pete
Peter Duniho - 22 May 2007 17:46 GMT
> 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
Walter Wang [MSFT] - 22 May 2007 04:42 GMT
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 (wawang@online.microsoft.com, 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.