Jump to content
Xtreme .Net Talk

wait for process


Recommended Posts

Guest greifedc
Posted

I am looping system.diagnostics.process.start this code works when I put a break in the code and single step it thru waiting for each line to finish. but if I don't use a break or step it thru quickly the process I am trying to start doesn't have a chance to finish the first time around before it loops and is ready to start again. Is there a way to make it wait for a process before moving on to the next?

 

Dim ProgramsSelected As Int16

Dim count As Int16 = 0

ProgramsSelected = newProgramList.SelectedItems.Count

Do

count += 1

System.Diagnostics.Process.Start _(newProgramList.SelectedItem)

newProgramList.SetSelected _(newProgramList.SelectedIndex, False)

Loop Until count = ProgramsSelected

 

:confused:

  • 2 weeks later...
  • Administrators
Posted

try altering to the following code.

Notice the p.WaitForExit method after p.Start

 


Dim ProgramsSelected As Int16
Dim count As Int16 = 0
ProgramsSelected =  newProgramList.SelectedItems.Count

Do
   count += 1

Dim p As New  System.Diagnostics.Process()
		p.Start(newProgramList.SelectedItem)
		p.WaitForExit()

newProgramList.SetSelected _(newProgramList.SelectedIndex, False)
	Loop Until count = ProgramsSelected

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • *Gurus*
Posted

A nicer way would be to use the event-driven method:

 

   Dim WithEvents myProcess As Process

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       myProcess = Process.Start("c:\windows\notepad.exe")
       myProcess.EnableRaisingEvents = True
   End Sub

   Private Sub myProcess_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles myProcess.Exited
       MessageBox.Show("Exited!")
   End Sub

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

  • 1 year later...
Posted
A nicer way would be to use the event-driven method:

 

   Dim WithEvents myProcess As Process

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       myProcess = Process.Start("c:\windows\notepad.exe")
       myProcess.EnableRaisingEvents = True
   End Sub

   Private Sub myProcess_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles myProcess.Exited
       MessageBox.Show("Exited!")
   End Sub

 

If I understand this correctly, the process will not fire again until it is completed? Even if it is in a loop? I have an application that downloads an unknow number of files, has to decrypt each one, then process them. So as long as I use this, it should wait for the process to complete on each file?

"Nobody knows what I do until I stop doing it."

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...