Process-need to wait for completion

ccsdev

Member
Joined
Aug 21, 2009
Messages
5
Programming Experience
Beginner
I am a beginner so please be patient. I am trying to solve problems with existing code. We have a tree view where user may check off multiple files (tif,pdf, doc etc) for printing. When the print buttton is clicked each document should be printed. Each document must be printed in full before the next one starts. The docs are not printing in full or in order. I have attached the code for review.

Private Sub RecurseNodes(ByVal col As TreeNodeCollection)
For Each tn As TreeNode In col
If tn.Tag = "FILE" And tn.Checked Then
wComplete = False
If (tn.FullPath.Length > 0) Then
Try
Dim WordProcess As Process = New Process
WordProcess.StartInfo.FileName = tn.FullPath
WordProcess.StartInfo.Verb = "Print"
WordProcess.StartInfo.CreateNoWindow = True
WordProcess.StartInfo.UseShellExecute = True
WordProcess.EnableRaisingEvents = True
AddHandler WordProcess.Exited, AddressOf Me.ProcessExited
WordProcess.Start()
If wComplete = True Then
tn.Checked = False
End If

Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "TIM")
End Try
Else
End If
End If

If tn.Nodes.Count > 0 Then
RecurseNodes(tn.Nodes)
End If
Next
End Sub

Friend Sub ProcessExited(ByVal sender As Object, ByVal e As System.EventArgs)
Dim WordProcess As Process = DirectCast(sender, Process)
WordProcess.Close()
wComplete = True
End Sub

I have tried several things to make this work including adding WaitToExit after the Start. It appears to me that the "print" process whether Adobe (pdf) or Microsoft Imaging (tif) is not really exiting. Can anyone help?
Thanks
 
Forget handling the Exited event of your Process. Call Process.Start and then Process.WaitForExit. WaitForExit will block until the process you started has exited, at which point your app will continue and print the next file.
 
Hung the program

I added WaitForExit after the start, commented out the AddHandler and recompiled. The progam just hangs on the first document, I don't really think there is an "exit". I am a beginner in vb but as an old DOS programmer this looks like the software (Adobe, MS Image etc) is not really being opened and exited. This is causing major problems at the client site, they expect the documents to be printed in a specific order.
 
Back
Top