How do I differentiate between a process running & an app visible on screen

x38class-9313 141 Reputation points
2021-10-04T02:34:12.587+00:00

Norton Security is part of Windows start up, therefore it is always running as a process.
If a user selects Norton icon on desktop, Norton window appears.
On closing the window it only hides the window as distinct from closing the app. (because it is always running)
I need to determine if the user has woken the app to show on the screen & also know when the app is not visible on the screen when the user closes the app.
ie, What is code to indicate the app screen is visible or not visible

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,579 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 40,651 Reputation points
    2021-10-07T10:18:49.907+00:00

    For applications that use multiple processes (e.g., chrome, firefox, etc.) there is no guarantee the FirstOrDefault() will return a process where Process.MainWindowHandle contains a valid Window handle. For example, consider the following console application -

    Module Module1  
      
        <DllImport("user32.dll", SetLastError:=True)>  
        Function IsWindowVisible(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean  
        End Function  
      
        Sub Main()  
            Dim procs As Process() = Process.GetProcessesByName("Firefox")  
            For Each proc As Process In procs  
                If proc.MainWindowHandle <> 0 Then  
                    Console.WriteLine("Firefox process id {0} MainWindowHandle visible: {1}", proc.Id, IsWindowVisible(proc.MainWindowHandle))  
                Else  
                    Console.WriteLine("Firefox process id {0} MainWindowHandle is 0", proc.Id)  
                End If  
            Next  
      
        End Sub  
      
    End Module  
      
    

    Task manager showing the Firefox processes -

    138447-taskmanager.png

    Console application output -

    138448-vbvis-results.png

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2021-10-05T06:42:02.603+00:00

    @x38class-9313 , based on my research, I suggest that you could try the Windows API IsWindowVisible function to check if the app is visible or not.

    I make a sample wpf app like your Norton Security.

    Here is a code example you could refer to.

     <DllImport("user32.dll", SetLastError:=True)>  
    Private Shared Function IsWindowVisible(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean  
    End Function  
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        Dim currentProcess As Process = Process.GetProcessesByName("WpfApp1").FirstOrDefault()  
        Dim t As Boolean = IsWindowVisible(currentProcess.MainWindowHandle)  
        MessageBox.Show(t.ToString())  
      
    End Sub  
    

    Result:

    137665-2.gif


    If the response 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.


  2. x38class-9313 141 Reputation points
    2021-10-07T02:43:58.607+00:00

    Again, thanks for a further suggestion, however as I said before, "Norton Security" is always running so using task manager is not an option.

    this code has no fault:
    Dim currentProcess As Process = Process.GetProcessesByName("chrome")

    however:
    Dim currentProcess As Process = Process.GetProcessesByName("chrome").FirstOrDefault()

    fails because there is no "FirstOrDefault()" option to add to this line of code

    your example seems everything I want except for this extension at the end of line that is not available, ie .FirstOrDefault()


  3. x38class-9313 141 Reputation points
    2021-10-08T03:04:05.487+00:00

    RLWA32-6355, Thanks for that, works fine for me as I just tested it, now to put it to a real test in my Release to users.

    I dont see where I can credit you with accepting your answer

    For, JackJJun-MSFT, obviously you have put in some effort to try & solve for me, much appreciated


  4. x38class-9313 141 Reputation points
    2021-10-10T02:33:29.84+00:00

    Whilst I have tested and accepted an answer, I find for my purpose there is an issue where there is more than one instance of the process, to resolve this I have decided on using the following which should take care of more than one process running, hope it may be useful to others

    Dim K As Byte
    Dim J As Byte
    Dim Status(10) As String
    Dim Result As String

        Dim prog As New Process
        For Each prog In Process.GetProcesses
            If UCase(prog.ProcessName) = UCase("NortonSecurity") Then
                K += 1
                Dim t As Boolean = IsWindowVisible(prog.MainWindowHandle)
                Status(K) = t.ToString()
            End If
        Next
    
        For J = 1 To K
            If Status(J) = "True" Then
                Result = Status(J)
                Exit For
            End If
        Next
    
        If Result = "" Then
            MsgBox("Not Visible")
        Else
            MsgBox("Visible")
        End If