5

I am using autohotkey to do some automate process.

I need help with closing chrome.exe

I tried

        Process, Close,  chrome.exe
;    or
        Run taskkill /im chrome.exe

but it give me chrome crashed error when I start it again.

Thanks

Paul Irish
  • 47,354
  • 22
  • 98
  • 132
Mowgli
  • 3,422
  • 21
  • 64
  • 88

3 Answers3

7

Use WinClose to send a close message to the window, rather than killing the process:

SetTitleMatchMode 2
WinClose Google Chrome
mihai
  • 37,072
  • 9
  • 60
  • 86
1

mihai's helpful answer works well if a single Chrome window (typically containing multiple tabs) is open.

However, more work is needed if you want to close all open Chrome windows:

; Get all hWnds (window IDs) created by chrome.exe processes.
WinGet hWnds, List, ahk_exe chrome.exe

; Loop over all hWnds and close them.
Loop, %hWnds%
{
  hWnd := % hWnds%A_Index% ; get next hWnd
  WinClose, ahk_id %hWnd%
}

Also note that the WinClose documentation calls using WinClose's use of the WM_CLOSE message "somewhat forceful" and suggests the following PostMessage alternative, which mimics the user action of pressing Alt-F4 or using the Window menu's Close item:

; Get all hWnds (window IDs) created by chrome.exe processes.
WinGet hWnds, List, ahk_exe chrome.exe

; Loop over all hWnds and close them.
Loop, %hWnds%
{
  hWnd := % hWnds%A_Index% ; get next hWnd
  PostMessage, 0x112, 0xF060,,, ahk_id %hWnd%
}
Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

Here is a function for 'soft closing' specific processes:

CloseAllInstances( exename )
{
    WinGet LhWnd, List, % "ahk_exe " exename
    Loop, %LhWnd%
        PostMessage, 0x112, 0xF060,,, % "ahk_id " LhWnd%A_Index%
}

So you simply need to call the following line to close all chrome instances:

CloseAllInstances( "chrome.exe" )