经常有需要把后台的自动程序重启。一直用的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