Problem with HasExited and WaitForExit()
-
I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text);
Please Help! Thanks! -
I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text);
Please Help! Thanks!danielhasdibs wrote:
HasExited is false
That's as it should be.
-
danielhasdibs wrote:
HasExited is false
That's as it should be.
Ok, would you care to explain further, please? Why is HasExited false when the user has not exited the process?
-
Ok, would you care to explain further, please? Why is HasExited false when the user has not exited the process?
To indicate such. Property Value true if the operating system process referenced by the Process component has terminated; otherwise, false. Remarks A value of true for HasExited indicates that the associated process has terminated
modified on Friday, February 8, 2008 3:11 PM
-
I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text);
Please Help! Thanks!Ok, is there anyone out there that actually has a solution to the problem?
-
Ok, is there anyone out there that actually has a solution to the problem?
danielhasdibs, I don't have a solution, but i have an idea. Start a new thread that is passed the process info that you start first, and then do a while loop. eg:
do
{
somet();
}
while (!process.HasExited)LaunchUrl();
Only an idea and someone might point out something i have not thought of but have a go and see what happens. Regards, Gareth.
-
Ok, is there anyone out there that actually has a solution to the problem?
Not with the approach you are using. It really is a pain to wait for thread exits in Windows. I usually give up in frustration and rely on load bearing for loops (yuk!).
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway -
Ok, would you care to explain further, please? Why is HasExited false when the user has not exited the process?
-
I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text);
Please Help! Thanks!danielhasdibs wrote:
psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0];
I'm wondering about these lines; why the at signs? Why the [0]? Does Word start as expected?
WaitForExit()
ought to do what you want. -
danielhasdibs wrote:
psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0];
I'm wondering about these lines; why the at signs? Why the [0]? Does Word start as expected?
WaitForExit()
ought to do what you want.I have tried many, many variations of the code above. I'm practically bald from me pulling my hair out. I used the @ symbol because I saw it used in other code, but even without it, HasExited and WaitForExit do not work as expected. Ignore the string array and just assume it is a string (which it is). That's just how I need it for my particular piece of code. However, the result of the code above are as follows: The Word document opens, then immediately the hyperlink opens. There is no pause or anything.
-
danielhasdibs, I don't have a solution, but i have an idea. Start a new thread that is passed the process info that you start first, and then do a while loop. eg:
do
{
somet();
}
while (!process.HasExited)LaunchUrl();
Only an idea and someone might point out something i have not thought of but have a go and see what happens. Regards, Gareth.
I tried it without success. Once again, the two applications started: one right after the other. If you have another idea, I'm all ears. Thanks.
-
I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text);
Please Help! Thanks!I don't know if it makes a difference, but I'm using MS Visual C# Express 2005. If someone could post a code sample that works for them, that would be awesome! Thanks. UPDATE By the way, please use MS Word as the first application to open and a hyperlink as the second. That seems to make a difference in the code.
modified on Friday, February 8, 2008 4:12 PM
-
I have tried many, many variations of the code above. I'm practically bald from me pulling my hair out. I used the @ symbol because I saw it used in other code, but even without it, HasExited and WaitForExit do not work as expected. Ignore the string array and just assume it is a string (which it is). That's just how I need it for my particular piece of code. However, the result of the code above are as follows: The Word document opens, then immediately the hyperlink opens. There is no pause or anything.
Have you stepped through the code with the debugger? You could add a
System.Threading.Thread.Sleep ( 1000 )
after the Start to give the process time to start. Otherwise I'm stumped. -
I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text);
Please Help! Thanks!Maybe this
WordProcess.EnableRaisingEvents = true;
is the problem, comment it out and try it again. Nope, it's not that either. :confused:modified on Friday, February 8, 2008 3:59 PM
-
I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text);
Please Help! Thanks!Dang. It works as expected with Notepad, but not with Word -- Word must act differently.
-
Dang. It works as expected with Notepad, but not with Word -- Word must act differently.
Bummer! If you or anyone else knows how I can keep track of (tell when it's closed by the user) a Word document, I would be greatly appreciative!
-
HasExited... = Has the user exited? The user HASN'T exited (false) The user HAS existed (true) Therefore, when HasExited==true then the application has exited!
For some reason, this does not work with MS Word. (at least on my computer)
-
Dang. It works as expected with Notepad, but not with Word -- Word must act differently.
If you can get it to work the other way around (hyperlink opens first, waits for the user to exit, then opens the Word document) that would work for me, too. thanks.
-
Bummer! If you or anyone else knows how I can keep track of (tell when it's closed by the user) a Word document, I would be greatly appreciative!
More info: Outlook uses Word as its editor so when Outlook is open, Word is running, so then the Process.Start merely passes the file to the running instance of Word and exits. Close Outlook, and try again. It worked for me that way, but I still don't have a decent workaround.
-
I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text);
Please Help! Thanks!After
danielhasdibs wrote:
WordProcess = System.Diagnostics.Process.Start(psi);
add
WordProcess.StandardOutput.ReadToEnd();
Works on my system (no need for the WaitForExit method call). For some reason the Exited event isn't raised but the HasExited property is correctly set. This is working...
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
psi.UseShellExecute = false;
psi.Arguments = "Help.txt";
psi.FileName = "C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE";System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess.EnableRaisingEvents = true; WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.StandardOutput.ReadToEnd(); Console.WriteLine(WordProcess.HasExited); System.Diagnostics.Process.Start("Explorer.exe", "http://google.com");
Dave
modified on Friday, February 8, 2008 5:27 PM