Program to run a query and end another program

BNE Sys 61 Reputation points
2022-08-01T00:23:29.363+00:00

I have a live emergency map that runs all the time. However, the same computer also shows promotional presentations on the reception TV. In case of an alert (logs fetched by the SQL query, which is working fine), I want this VB program to run the query every 5 seconds, [U]if any result returns[/U], should close the abcd123.exe and bring the zxy.exe (emergency map window) to the front.

Public Class Form1  
    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  
  
        If SQL.HasException(True) Then Exit Sub  
  
        dgvData.DataSource = SQL.DBDT  
    End Sub  
  
End Class  

226525-screenshot.jpg

Developer technologies | Visual Basic for Applications
Developer technologies | VB
0 comments No comments
{count} votes

Answer accepted by question author
  1. Jiachen Li-MSFT 34,231 Reputation points Microsoft External Staff
    2022-08-01T09:25:07.037+00:00

    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.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. BNE Sys 61 Reputation points
    2022-08-04T04:15:11.227+00:00

    Hi, Not sure, but still not closing the notepad and bringing the excel in front, despite the data correctly fetched and keyword present.

    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 '%Access%' Order By [UTCTimeInserted] Desc;")  
            Else  
                SQL.ExecQuery(Query)  
            End If  
            ' ERROR HANDLING  
            If SQL.HasException(True) Then  
                Timer1.Stop()  
                KillHungProcess("Notepad.exe")  
                StartOrShowProcess("Excel.exe")  
            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  
    

  2. BNE Sys 61 Reputation points
    2022-08-05T06:19:53.7+00:00

    How do I do that? Where should paste the line? Thank you

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.