Problem reusing Processes and Disposed not firing

Sprint

Well-known member
Joined
Feb 3, 2006
Messages
58
Location
Ohio
Programming Experience
5-10
Got a little issue with a small program I'm writing. I'm attempting to run different tasks in there own processes, waiting until each is done while keeping the UI responsive. So far it works exactly how I want it until I try to reuse some of those processes....here is a sample of the code:

VB.NET:
Private WithEvents TaskOneProcess As Process
Private WithEvents TaskTwoProcess As Process
Private WithEvents TaskThreeProcess As Process
Private CurrentProcess as Integer = 0

Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click
      CurrentProcess=1
      StartButton.Enabled = False
      Call StartFixing()
End Sub

Private Sub StartFixing()
        Select Case CurrentProcess
            Case 1
                CurrentProcess = 2
                Call TaskOne
            Case 2
                CurrentProcess = 3
                Call TaskTwo
            Case 3
                CurrentProcess = 4
                Call TaskThree
            Case 4
                StartButton.Enabled = True
                CurrentProcess = 0
        End Select
End Sub

Private Sub TaskOne()
    Using TaskOneProcess
        ' Do a bunch of stuff here
    End Using
End Sub

Private Sub TaskOneProcess_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles TaskOneProcess.Disposed
    Call StartFixing()
End Sub

Private Sub TaskTwo()
    Using TaskTwoProcess
        ' Do a bunch of stuff here
    End Using
End Sub

Private Sub TaskTwoProcess_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles TaskTwoProcess.Disposed
    Call StartFixing()
End Sub

Private Sub TaskThree()
    Using TaskThreeProcess
        ' Do a bunch of stuff here
    End Using
End Sub

Private Sub TaskThreeProcess_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles TaskThreeProcess.Disposed
    Call StartFixing()
End Sub

This is a over simplification of what's going on but it gets the point across. So it works exactly as expected, a button is pressed and the sequence starts. As each task gets disposed it recalls the StartFixing statement which then calls the next one in order until they are all done. BUT when I go to do it a second time the Disposed never fires assuming since it's already disposed. How do I recreate the same named process so it can be reused and still have it fire the disposed event?

-Allan
 
You have to create a new Process instance and assign it to the WithEvents variable, pretty much the same that was done first time.
 
Ok, that makes sense....I'm declaring each as a new process at application startup and then they gets disposed. So they were disposed when it runs the second time the WithEvents never fires. I tried a bunch of stuff but got it working with this:

VB.NET:
Private Sub TaskOne()
   TaskOneProcess= New Process
   TaskOneProcess.EnableRaisingEvents = True
   Using TaskOneProcess
        ' Do a bunch of stuff here
   End Using
End Sub

So this way the new event is create before each is used and the EnableRaisingEvents makes sure the disposed is firing. Thanks,

-Allan
 
Back
Top