Joshua
Joshua

Reputation: 43327

Process.WaitForExit(n) won't wait immediately after Process.Start()

Code:

                                    using p = new Process();
                                    p.StartInfo.UseShellExecute = true;
                                    p.StartInfo.Verb = "printto";
                                    p.StartInfo.FileName = fileName;
                                    p.StartInfo.Arguments = printerName;
                                    p.Start();
                                    p.WaitForExit(20000);

Sometimes throws:

"System.InvalidOperationException: No process is associated with this object." from the .WaitForExit(20000) line.

We have this captured from the event log so it really did happen just like this. I'm baffled how we could be in the "no process" state immediately after p.Start(). I have managed to make unrunnable processes that fail to ever run but get past the p.Start() preflight checks but they always get so far as creating a handle so .WaitForExit() always works by reporting it's already exited.

I'm baffled. It would be plausible that ShellExecuteEx called some kind of DDE function or something else that just doesn't identify processes, but the Process API doesn't seem to support that operation. The calling code doesn't have a window open at all either, so what's the DDE source?

I don't actually need to wait here; I just need to detect if it was some DDE or other nonsense and could otherwise do what I need with a small sleep.

There's a near-dupe that ended up with an answer of invoking the iexplore process directly, which I can't use.

Upvotes: 0

Views: 82

Answers (1)

CraftingDragon007
CraftingDragon007

Reputation: 11

You need to make sure that the FilePath points to a printable Document (like a pdf doc or rtf). And put the printerName in Phrases p.StartInfo.Arguments = "\"" + printerName + "\""; else the printer will throw an error.

Upvotes: 0

Related Questions