Hi @BNE Sys ,
You can refer to the following two pieces of code to close the process and bring the window to the front.
Public Class Inventory
Public SQL As New SQLControl()
Public Sub LoadGrid(Optional Query As String = "")
If Query = "" Then
SQL.ExecQuery("SELECT TOP 100 * FROM VReview WHERE (Text LIKE '%Help%' or Text LIKE '%Access%') Order By [UTCTimeInserted] Desc;")
Else
SQL.ExecQuery(Query)
End If
' ERROR HANDLING
If SQL.HasException(True) Then
Timer1.Stop()
KillHungProcess("abcd123.exe")
StartOrShowProcess("zxy")
End If
dgvData.DataSource = SQL.DBDT
End Sub
Private Sub Inventory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MdiParent = Form1
LoadGrid()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 500
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
LoadGrid()
End Sub
Public Sub KillHungProcess(processName As String)
Dim psi As ProcessStartInfo = New ProcessStartInfo
psi.Arguments = "/im " & processName & " /f"
psi.FileName = "taskkill"
psi.WindowStyle = ProcessWindowStyle.Hidden
Dim p As Process = New Process()
p.StartInfo = psi
p.Start()
End Sub
#Region "DLL Imports"
<System.Runtime.InteropServices.DllImport("User32.dll")>
Private Shared Function SetForegroundWindow(handle As IntPtr) As Boolean
End Function
<System.Runtime.InteropServices.DllImport("User32.dll")>
Private Shared Function ShowWindow(handle As IntPtr, nCmdShow As Integer) As Boolean
End Function
<System.Runtime.InteropServices.DllImport("User32.dll")>
Private Shared Function IsIconic(handle As IntPtr) As Boolean
End Function
#End Region
Private Sub StartOrShowProcess(ByVal strProcessName As String)
Try
Dim handle As IntPtr
Dim proc As Process() = Process.GetProcessesByName(strProcessName)
If proc.Count > 0 Then
For Each procP As Process In proc
handle = procP.MainWindowHandle
If handle <> 0 AndAlso IsIconic(handle) Then 'Do we have a handle and is it minimized?
ShowWindow(handle, 9)
SetForegroundWindow(handle)
End If
Next
Else 'Not running or started...
Process.Start(strProcessName)
End If
Catch ex As Exception
'Handle your error...
End Try
End Sub
End Class
Best Regards.
Jiachen Li
----------
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.