【问题标题】:vb.net process class with process.exited event not reached未达到带有 process.exited 事件的 vb.net 进程类
【发布时间】:2016-04-21 11:43:28
【问题描述】:

我基于这个 msdn page 创建了一个 class,唯一的问题是我的 process.exited 事件永远无法到达。

进程的.exe 是我创建的控制台应用程序。完成后它会自行关闭。但是我的事件没有到达,我必须等待 30 秒才能恢复代码。

我做错了什么吗?对于我的测试用例,.exe 只需 +- 5 秒即可完成。

我假设当控制台应用程序在代码完成后关闭时,进程退出。我的这个假设是错误的?

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

补充:

答案可能在这篇文章中给出,但在c# 中,我不明白。

c# possible 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

【问题讨论】:

    标签: vb.net class events process


    【解决方案1】:

    你可以替换这部分代码:

    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
    

    与:

         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 方法非常有用,它会冻结你的代码,直到给定的软件完成。如果运行较长的进程,可能需要后台工作人员。

    【讨论】:

    • 谢谢,这样更好。但我仍然不太明白为什么事件没有发生。不过这个版本更好!
    • 你在oProcess.Start()之前错过了这一行oProcess.EnableRaisingEvents = True
    • 很高兴我太仓促而没有看到,否则我不会知道WaitForExit
    猜你喜欢
    • 2013-07-29
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多