VB.Net Close Web Brosers - Taskkill

~OSD~ 2,201 Reputation points
2021-01-06T09:44:58.62+00:00

Hi,

I am using following way to "Close" the web browsers (Edge, Chrome, Firefox and IE). I am able to achieve it via "taskkill.exe"

 Dim TskKill_Chrome As New ProcessStartInfo("Taskkill.exe")  
        TskKill_Chrome.Arguments = "/F /IM Chrome.exe /T"  
        Process.Start(TskKill_Chrome)  

Dim TskKill_Edge As New ProcessStartInfo("Taskkill.exe")  
        TskKill_Edge.Arguments = "/F /IM MSEdge.exe /T"  
        Process.Start(TskKill_Edge)  

The downside is the user experience, which complains that your previous session wasn't successfully shutdown.
For example:

53947-image.png

53948-image.png

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

Accepted answer
  1. Xingyu Zhao-MSFT 5,371 Reputation points
    2021-01-07T06:46:14.747+00:00

    Hi @~OSD~ ,
    Also try:

            Dim AllProcesses As Process() = Process.GetProcesses()  
            For Each process In AllProcesses  
                If process.MainWindowTitle <> "" Then  
                    Dim name As String = process.ProcessName  
                    If name = "iexplore" OrElse name = "msedge" OrElse name = "firefox" OrElse name = "chrome" Then  
                        process.CloseMainWindow()  
                    End If  
                End If  
            Next  
    

    The request to exit the process by calling CloseMainWindow does not force the application to quit. The application can ask for user verification before quitting, or it can refuse to quit.

    Best Regards,
    Xingyu Zhao
    *
    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.

1 additional answer

Sort by: Most helpful
  1. Castorix31 88,381 Reputation points
    2021-01-06T10:08:33.463+00:00

    If I close MsEdge with WM_CLOSE, I don't have this message (Windows 10) =>

    Dim processArray As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("msedge")
    Dim hWndMain As IntPtr = IntPtr.Zero
    If (processArray IsNot Nothing) Then
        For Each proc As System.Diagnostics.Process In processArray
            If (proc.MainWindowHandle <> IntPtr.Zero) Then
                hWndMain = proc.MainWindowHandle
                Exit For
            End If
        Next
        If (hWndMain <> IntPtr.Zero) Then
            PostMessage(hWndMain, WM_CLOSE, 0, IntPtr.Zero)
        End If
    End If
    

    Declarations :

    <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal msg As UInteger, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    End Function
    
    Public Const WM_CLOSE = &H10
    
    0 comments No comments

Your answer

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