Mech_Engineer
Mech_Engineer

Reputation: 555

vb.net process class with process.exited event not reached

I have created a class based on this msdn page the only problem my process.exited event is never reached.

The .exe for the process is a console application that I have created. And it closes it self when it's done. But my event does not get reached and I have to wait 30s for the code to resume.

Is there something that i am doing wrong? For my test case the .exe only takes +- 5 seconds to complete.

I assume when a console application closes after the code is done the process is exited. My this assumption is wrong?

Imports System
Imports System.Diagnostics
Imports System.Threading

Public Class CopyDesignProcessClass

    Private WithEvents oProcess As New Process()
    Private elapsedTime As Integer
    Private eventHandled As Boolean

    Public Event Exited As EventHandler

    Shared Sub Start(ByVal oArgument As String)

        ' Verify that an argument has been entered.
        If oArgument Is Nothing Or oArgument = "" Then
            Exit Sub
        End If

        ' Create the process and copy the design.
        Dim myCopyDesignProcess As New CopyDesignProcessClass
        myCopyDesignProcess.CopyDesign(oArgument)
    End Sub

    ' Print a file with any known extension.
    Sub CopyDesign(ByVal oArgument As String)

        elapsedTime = 0
        eventHandled = False

        Try
            ' Start a process to copy a design and raise an event when done.
            oProcess.StartInfo.FileName = "C:\Program Files\Autodesk\CopyDesign Tool\CopyDesignTool.exe"
            oProcess.StartInfo.Arguments = oArgument
            oProcess.Start()


        Catch ex As Exception
            MsgBox("An error occurred trying to copydesign " & oArgument & " :" &
                vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "CopyDesign Failed")
            Return
        End Try

        ' Wait for Exited event, but not more than 30 seconds.
        Const SLEEP_AMOUNT As Integer = 100
        Do While Not eventHandled
            elapsedTime += SLEEP_AMOUNT
            If elapsedTime > 30000 Then
                Exit Do
            End If
            Thread.Sleep(SLEEP_AMOUNT)
        Loop
    End Sub

    ' Handle Exited event and display process information.
    Private Sub myProcess_Exited(ByVal sender As Object,
                ByVal e As System.EventArgs) Handles oProcess.Exited

        eventHandled = True
        Debug.Print("Exit time:    {0}" & vbCrLf &
                    "Exit code:    {1}" & vbCrLf &
                    "Elapsed time: {2}",
                    oProcess.ExitTime, oProcess.ExitCode, elapsedTime)
    End Sub

End Class

Additional:

The answer may be given in this post but it's in c# and this I don't understand.

c# possible answer


Corrected code, but used another solution posted as answer.

Imports System
Imports System.Diagnostics
Imports System.Threading

Public Class CopyDesignProcessClass

    Private WithEvents oProcess As New Process()
    Private elapsedTime As Integer
    Private eventHandled As Boolean

    Public Event Exited As EventHandler

    Shared Sub Start(ByVal oArgument As String)

        ' Verify that an argument has been entered.
        If oArgument Is Nothing Or oArgument = "" Then
            Exit Sub
        End If

        ' Create the process and copy the design.
        Dim myCopyDesignProcess As New CopyDesignProcessClass
        myCopyDesignProcess.CopyDesign(oArgument)
    End Sub

    ' Print a file with any known extension.
    Sub CopyDesign(ByVal oArgument As String)

        elapsedTime = 0
        eventHandled = False

        Try
            ' Start a process to copy a design and raise an event when done.
            oProcess.StartInfo.FileName = "C:\Program Files\Autodesk\CopyDesign Tool\CopyDesignTool.exe"
            oProcess.StartInfo.Arguments = oArgument
            oProcess.EnableRaisingEvents = True 
            oProcess.Start()


        Catch ex As Exception
            MsgBox("An error occurred trying to copydesign " & oArgument & " :" &
                vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "CopyDesign Failed")
            Return
        End Try

        ' Wait for Exited event, but not more than 30 seconds.
        Const SLEEP_AMOUNT As Integer = 100
        Do While Not eventHandled
            elapsedTime += SLEEP_AMOUNT
            If elapsedTime > 30000 Then
                Exit Do
            End If
            Thread.Sleep(SLEEP_AMOUNT)
        Loop
    End Sub

    ' Handle Exited event and display process information.
    Private Sub myProcess_Exited(ByVal sender As Object,
                ByVal e As System.EventArgs) Handles oProcess.Exited

        eventHandled = True
        Debug.Print("Exit time:    {0}" & vbCrLf &
                    "Exit code:    {1}" & vbCrLf &
                    "Elapsed time: {2}",
                    oProcess.ExitTime, oProcess.ExitCode, elapsedTime)
    End Sub

End Class

Upvotes: 2

Views: 2226

Answers (1)

Claudius
Claudius

Reputation: 1911

you can replace this part of the code:

Try
            ' Start a process to copy a design and raise an event when done.
            oProcess.StartInfo.FileName = "C:\Program Files\Autodesk\CopyDesign Tool\CopyDesignTool.exe"
            oProcess.StartInfo.Arguments = oArgument
            oProcess.Start()


        Catch ex As Exception
            MsgBox("An error occurred trying to copydesign " & oArgument & " :" &
                vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "CopyDesign Failed")
            Return
        End Try

        ' Wait for Exited event, but not more than 30 seconds.
        Const SLEEP_AMOUNT As Integer = 100
        Do While Not eventHandled
            elapsedTime += SLEEP_AMOUNT
            If elapsedTime > 30000 Then
                Exit Do
            End If
            Thread.Sleep(SLEEP_AMOUNT)
        Loop

with:

     Try
        ' Start a process to copy a design and raise an event when done.
        oProcess.StartInfo.FileName = "C:\Program Files\Autodesk\CopyDesign Tool\CopyDesignTool.exe"
        oProcess.StartInfo.Arguments = oArgument
        oProcess.Start()
        oProcess.WaitForExit()

    Catch ex As Exception
        MsgBox("An error occurred trying to copydesign " & oArgument & " :" &
            vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "CopyDesign Failed")
        Return
    End Try

waitforexit method is very useful, it will freeze your code until given software is done. If running longer processes background worker may be necessary.

Upvotes: 2

Related Questions