当前位置: 代码迷 >> 综合 >> .net application重启之旧程序的清理
  详细解决方案

.net application重启之旧程序的清理

热度:60   发布时间:2024-01-24 20:30:14.0

经常有需要把后台的自动程序重启。一直用的application.restart()

但是,多线程的程序总有不明原因清理不干净,用了       

        timr.Stop()
timr.Dispose()
timr = Nothing

仍然不行,后来想到了用Kill

        Dim myid As Integer
myid = Process.GetCurrentProcess.Id
If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1 Then
Threading.Thread.Sleep(2000)   '等一会,可能是旧进程还未退出
End If
If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1 Then
Dim myProcesses() As Process
Dim myProcess As Process
myProcesses = Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName)
For Each myProcess In myProcesses
'If myProcess.StartTime < Now.AddSeconds(-30) Then
If myProcess.Id <> myid Then
myProcess.Kill()
End If
Next
End If

  相关解决方案