代码之家  ›  专栏  ›  技术社区  ›  John89

系统诊断学。应用程序关闭后进程未正确关闭

  •  0
  • John89  · 技术社区  · 4 年前

    我一直在试图找到解决问题的办法 ExitTool 关闭主应用程序后,我正在使用的未正确关闭。现在的情况是,当我关闭应用程序时,ExifTool仍在后台运行,我必须手动杀死它。

    enter image description here


    下面是流程启动的代码片段。

    Public Shared Sub ExecuteExifTool()
        If ExifToolStarted Then Exit Sub
        ExifToolStarted = True
        Dim NowString As String = Date.Now.ToString("yyyyMMddHHmmss")
        If Not IO.Directory.Exists(".\Runtime") Then IO.Directory.CreateDirectory(".\Runtime")
        Dim GetDirectory As New IO.DirectoryInfo(".\Runtime")
        If GetDirectory.GetFiles.Count > 0 Then
            For Each i As IO.FileInfo In GetDirectory.GetFiles
                If i.FullName.Contains("exif.Yv") AndAlso i.FullName.Contains("-cL") AndAlso i.FullName.Contains(".exe") Then : Try : i.Delete() : Catch : End Try : End If
            Next
        End If
        HostName = ".\Runtime\exif.Yv" & NowString.Substring(0, 8) & "-cL" & NowString.Substring(8, 6) & ".exe"
        IO.File.Copy(".\exiftool.exe", HostName)
        Using ExifToolProcess As New Process
            With ExifToolProcess
                .StartInfo.RedirectStandardInput = True
                .StartInfo.FileName = HostName
                .StartInfo.UseShellExecute = False
                .StartInfo.Arguments = "-stay_open" & " True -@ " & "-"
                .StartInfo.RedirectStandardOutput = True
                .StartInfo.RedirectStandardError = True
                .StartInfo.CreateNoWindow = True
                .StartInfo.WindowStyle = ProcessWindowStyle.Hidden
                .Start()
                .BeginOutputReadLine()
                .BeginErrorReadLine()
            End With
        End Using
    End Sub
    

    解决这个问题的最新尝试是尝试并启动另一个可执行应用程序;本质上,这是另一个问题 Windows.Forms.Form 它会等待主应用程序关闭,然后尝试立即终止进程,然后自行处理。以下是片段。

    Public Class KillProcess
    
        Private _ProcessName As String
        Public Property ProcessName As String
            Get
                Return _ProcessName
            End Get
            Set(value As String)
                _ProcessName = value
            End Set
        End Property
    
        Private _MainApp As Form
        Public Property MainApplication As Form
            Get
                Return _MainApp
            End Get
            Set(value As Form)
                _MainApp = value
            End Set
        End Property
    
        Private Sub KillProcess_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            CleanupProcess()
        End Sub
    
        Public Sub CleanupProcess()
            While Not MainApplication.IsDisposed
                Dim FilesToDelete As New List(Of String)
                Dim ProcessesToKill As New List(Of Process)
                For Each p As Process In Process.GetProcesses
                    If p.ProcessName = ProcessName Then
                        FilesToDelete.Add(p.MainModule.FileName)
                        ProcessesToKill.Add(p)
                    End If
                Next
                For Each p As Process In ProcessesToKill
                    Try
                        p.Kill()
                        p.WaitForExit(10000)
                        p.Close()
                    Catch winException As System.ComponentModel.Win32Exception
                    Catch invalidException As InvalidOperationException
                    End Try
                Next
            End While
            Me.Dispose()
        End Sub
    End Class
    

    下面是这家初创公司的代码片段。

    Public Sub CleanupTask()
        Dim Handler As New Custodian.KillProcess With {.ProcessName = ExifToolHooker.HostName, .MainApplication = Me}
        Windows.Forms.Application.Run(Handler)
    End Sub
    
    Private Sub CloseApplication(sender As Object, e As FormClosingEventArgs) Handles Me.Closing
        Dim TaskHandler As Thread = New Thread(AddressOf CleanupTask)
        TaskHandler.SetApartmentState(ApartmentState.STA)
        TaskHandler.Start()
        ...
    End Sub
    
    0 回复  |  直到 4 年前
    推荐文章