How to know when external process has exited

K

kimiraikkonen

Hello,
I want to automate something and display a simple msgbox that
indicates my external process finished processing and "exited".

My code is that, what things should i add more:

Dim psInfo As New System.Diagnostics.ProcessStartInfo("bla.exe", "-b
")
psInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Normal
psInfo.WorkingDirectory = Application.StartupPath
psInfo.WindowStyle = ProcessWindowStyle.Normal
Dim myProcess As Process =
System.Diagnostics.Process.Start(psInfo)


Thanks...
 
H

Herfried K. Wagner [MVP]

kimiraikkonen said:
I want to automate something and display a simple msgbox that
indicates my external process finished processing and "exited".

My code is that, what things should i add more:

Dim psInfo As New System.Diagnostics.ProcessStartInfo("bla.exe", "-b
")
psInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Normal
psInfo.WorkingDirectory = Application.StartupPath
psInfo.WindowStyle = ProcessWindowStyle.Normal
Dim myProcess As Process =
System.Diagnostics.Process.Start(psInfo)

\\\
myProcess.WaitForExit()
....
///
 
K

kimiraikkonen

\\\
myProcess.WaitForExit()
...
///

Yes, but i've forgotten, i don't want to use waitforexit() because it
makes my project's main form unusable/hang while external process is
in progres.

Also i want to autoamate it, i don't know it's about looping
(iteration) or other. I don't want user prompt like button click. How?
I hope explained clear enough.
 
T

Tom Shelton

Yes, but i've forgotten, i don't want to use waitforexit() because it
makes my project's main form unusable/hang while external process is
in progres.

Also i want to autoamate it, i don't know it's about looping
(iteration) or other. I don't want user prompt like button click. How?
I hope explained clear enough.- Hide quoted text -

- Show quoted text -

Set the process objects EnableRaisingEvents, and then hook it's Exited
event.

Private WithEvents myProcess As New Process()

Private Sub DoCoolStuff()
With myProcess
.StartInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Normal
.StartInfo.WorkingDirectory = Application.StartupPath
.StartInfo.WindowStyle = ProcessWindowStyle.Normal
.EnableRaisingEvents = True
End With
myProcess.Start()
End Sub

Private Sub ProcessExited (Byval sender As Object, ByVal e As
System.EventArgs) Handles myProcess.Exited
' do cool stuff
End Sub

Anyway, not a complete working example, but it should give you the
idea.
 
R

rowe_newsgroups

Hello,
I want to automate something and display a simple msgbox that
indicates my external process finished processing and "exited".

My code is that, what things should i add more:

Dim psInfo As New System.Diagnostics.ProcessStartInfo("bla.exe", "-b
")
psInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Normal
psInfo.WorkingDirectory = Application.StartupPath
psInfo.WindowStyle = ProcessWindowStyle.Normal
Dim myProcess As Process =
System.Diagnostics.Process.Start(psInfo)

Thanks...

Perhaps you should re-read your previous posts. Armin has already told
you how to do this and even provided a sample....

http://groups.google.com/group/micr...nk=st&q=armin+process&rnum=1#bb15f878e987caaa

Thanks,

Seth Rowe
 
T

Tom Shelton

Seth,
Yes i know, but i couldn't manage to do. I added another "private sub
onProcess Exited..." as corrected by Visual Studio.
Then i run my project but at the end of processing, nothing happened.?- Hide quoted text -

- Show quoted text -

Did you set your process objects EnableRaisingEvetns property to
true? Otherwise, no event will be raised....
 
K

kimiraikkonen

Did you set your process objects EnableRaisingEvetns property to
true? Otherwise, no event will be raised....

Tom, thanks! That's it! It fixed determining about exiting event, but
another problem:

if i want to hide my visible progess bar by coding
"progressbar1.hide()" just before MsgBox("exited") i get that error:

"Cross-thread operation not valid: Control 'ProgressBar1' accessed
from a thread other than the thread it was created on."

How can i fix it though?

Very thanks
 
R

rowe_newsgroups

Tom, thanks! That's it! It fixed determining about exiting event, but
another problem:

if i want to hide my visible progess bar by coding
"progressbar1.hide()" just before MsgBox("exited") i get that error:

"Cross-thread operation not valid: Control 'ProgressBar1' accessed
from a thread other than the thread it was created on."

How can i fix it though?

Very thanks

You must marshal control back to the form. The easiest way (imo) is to
use Me.BeginInvoke.

Here's another sample that you need to place in the code-behind of
Form1 in a new Windows Application

////////////////////
Imports System.Threading

Public Class Form1
Private progress As ProgressBar

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
progress = New ProgressBar()
progress.Location = New Point(3, 3)
Me.Controls.Add(progress)

Dim button As New Button()
button.Location = New Point(3, 30)
button.Size = New Size(75, 23)
button.Text = "Begin Work"

AddHandler button.Click, AddressOf button_Click

Me.Controls.Add(button)
End Sub

Private Sub button_Click(ByVal sender As Object, ByVal e As
EventArgs)
Dim t As New Thread(AddressOf DoTheWork)
t.Start()
End Sub

Private Sub DoTheWork()
Try
'// Do something useful
MessageBox.Show("Doing something useful")
Finally
If Me.InvokeRequired Then
Dim d As New HideProgressBarDelegate(AddressOf
HideProgressBar)
Me.BeginInvoke(d)
Else
HideProgressBar()
End If
End Try
End Sub

Private Delegate Sub HideProgressBarDelegate()

Private Sub HideProgressBar()
progress.Hide()
End Sub

End Class
////////////////////

Thanks,

Seth Rowe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top