|
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)
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
|
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 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
|
|
|
|
|
Maybe this WordProcess.EnableRaisingEvents = true; is the problem, comment it out and try it again.
Nope, it's not that either.
modified on Friday, February 8, 2008 3:59 PM
|
|
|
|
|
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!
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
Hi,
this code works fine on WinXP:
Process proc=Process.Start(@"C:\test.doc");
log("procName="+proc.ProcessName);
proc.WaitForExit();
log("has exited");
provided no Word process is running yet (if there is, proc is null as per MSDN documentation).
PS: I do not use any Office Assistant, I have thrown it out long ago.
IIRC there is a small app that "helps" in launching Office components, if that were to intercept
the doc extension and then launch WinWord on its own, you would not get winword in procName;
you could fix that by explicitly starting winword, with the doc name as an argument tho).
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Process.WaitForExit(); and Process.StandardOutput.ReadToEnd(); seem to have the same result.
Any idea why the Exited event isn't fired Luc?
Dave
modified on Friday, February 8, 2008 5:44 PM
|
|
|
|
|
ReadToEnd() did not appear in the original post, nor in my only reply
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
No - I was just comenting that it appears to have the same result
Dave
|
|
|
|
|
danielhasdibs wrote: WaitForExit() does nothing
Nothing ever does "nothing". What does it do? Return immediately?
danielhasdibs wrote: System.Diagnostics.Process WordProcess = new System.Diagnostics.Process();
Why are you creating a new process here? You only throw it away in the next line where you create another new process.
danielhasdibs wrote: WordProcess = System.Diagnostics.Process.Start(psi);
You should check the return value. If a process is reused instead or starting a new, the return value is null.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Hi
Why did you give the wait for exit time as 10000 i.e WordProcess.WaitForExit(10000);
if you want the first application to be closed and open the second one do like this
WordProcess.WaitForExit();// wait for infinite
System.Diagnostics.Process.Start(txt_websiteAddress.Text);
And about HasExited property, if any of the word document is open other than the closed document, this property will not get updated. Since Word is single process which launch different instance of editor.
Thanks
Srini
|
|
|
|
|
Hi!
I just tried this and it works as expected using this code:
ProcessStartInfo psi = new ProcessStartInfo("C:\\Document.doc");
MessageBox.Show("Starting...");
Process p = Process.Start(psi);
p.WaitForExit();
MessageBox.Show("Finished");
The second message box appears as soon as I close Word.
Does this help?
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
|
Ok, I don't want to be one of those jerks who finds the answer and doesn't share so I have the working code here. I don't know why this didn't work before, but it didn't, and now it does, so I'm happy.
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();<br />
psi.RedirectStandardOutput = true;<br />
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;<br />
psi.UseShellExecute = false;<br />
psi.Arguments = txt_Wordfile.Text + " /n";<br />
psi.FileName = WordExe;
<br />
System.Diagnostics.Process WordProcess = new System.Diagnostics.Process();<br />
<br />
WordProcess = System.Diagnostics.Process.Start(psi);<br />
<br />
WordProcess.EnableRaisingEvents = true;<br />
while (!WordProcess.HasExited)<br />
{<br />
;<br />
}<br />
WordProcess.Kill(); <br />
<br />
<br />
System.Diagnostics.Process.Start(txt_website.Text);
Thanks everyone for your help!
|
|
|
|