Process.start() how to  
Author Message
AndyPham





PostPosted: Visual C# General, Process.start() how to Top

Hi All,

I'm using process.start() to execute msbuild.exe. Is there any properties or method that I could set or call to wait until it completely finished before the next line of code get executed

Best Regards,



Visual C#14  
 
 
ahmedilyas





PostPosted: Visual C# General, Process.start() how to Top

next line of... your C# code there is. you need to use a WaitForExit() method in the Process class. Example:

Process theProcess = new Process();

theProcess.StartInfo = new ProcessStartInfo("filename.ext");

theProcess.StartInfo.Arguments = "args here"

theProcess.WaitForExit();

theProcess.Start();

 

this will wait until the process has finished executing before running the next line of code in your C# app



 
 
Prabu.





PostPosted: Visual C# General, Process.start() how to Top

Hi,

I am using the following lines, but the process does not wait untill it closed.

foreach (FileInfo file in dir.GetFiles("*.csproj"))

{

Process theProcess = new Process();

theProcess.StartInfo = new ProcessStartInfo(file.FullName);

theProcess.StartInfo.Arguments = "";

theProcess.Start();

theProcess.WaitForExit();

}

Please let me know what should I did wrong.

Cheer.



 
 
ahmedilyas





PostPosted: Visual C# General, Process.start() how to Top

remove theProcess.Start();

 
 
Matthew Watson





PostPosted: Visual C# General, Process.start() how to Top

Process.WaitForExit() doesn't actually start a process. You really must call Process.Start() to start it.

I don't know why the OP's code is not working - it looks like it should (with the WaitForExit() call).

 
 
Prabu.





PostPosted: Visual C# General, Process.start() how to Top

When I remove theProcess.Start(); line then it throws, "No process is associated with this object." Exception. Here is the stack trace

at System.Diagnostics.Process.EnsureState(State state)
at System.Diagnostics.Process.EnsureState(State state)
at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.WaitForExit(Int32 milliseconds)
at System.Diagnostics.Process.WaitForExit()
at Project_Runner.Class1.Test(String path)



 
 
ahmedilyas





PostPosted: Visual C# General, Process.start() how to Top

yes sorry you're right, I was thinking of something else. I wouldnt understand why it wouldnt wait for exit...

 
 
Matthew Watson





PostPosted: Visual C# General, Process.start() how to Top

Actually, looking at the code, it's starting ".CSPROJ" files rather than executables.

Therefore, is it using the shell to start the application associated with ".CSPROJ" files

In which case, what may be happening is:

(1) Process.Start() starts a shell process.
(2) The shell process starts the application associated with ".CSPROJ" files.
(3) The shell process terminates (the application is still running).
(4) Process.WaitForExit() returns, because the shell process has terminated.

I'm pretty sure that's what's going on.

If I type the following at a command prompt (assuming there's a "Program.CS" in the current working directory):

start /wait program.cs

The "start" returns immediately, even though the application that loaded "program.cs" is still running.



 
 
ahmedilyas





PostPosted: Visual C# General, Process.start() how to Top

out of curiosity, what happens if you start the process with devenv and give the filename as an argument and start that process and wait for exit

 
 
Prabu.





PostPosted: Visual C# General, Process.start() how to Top

I need to do the automated testing with all the cs & vb projects. Just need to start the open/start the project file and then I will run it through the testing tool. The process should wait till the testing process completes. Is it possible Please suggest me.

Thanks for your golden time spent to me.



 
 
Prabu.





PostPosted: Visual C# General, Process.start() how to Top

Hi,

I would like to redirect the process output the console and write it into the file. I am using the following code. But it doesn’t work. Please let me know what I missed here.

string test = string.Format ( "\"{0}\" /build debug " , file.FullName , d.FullName + "\\devenv.log" );

Process projectProcess = new Process( );

projectProcess.StartInfo = new ProcessStartInfo( devLoc + "devenv.exe" , test );

Console.WriteLine ( file.FullName );

projectProcess.StartInfo.CreateNoWindow = true;

projectProcess.StartInfo.UseShellExecute = false;

projectProcess.StartInfo.ErrorDialog = true;

projectProcess.StartInfo.RedirectStandardOutput = true;

projectProcess.Start();

string r = projectProcess.StandardOutput.ReadLine();

Console.WriteLine ( r );