- TerminateProcess function (processthreadsapi.h)
- Syntax
- Parameters
- Return value
- Remarks
- Terminate method of the Win32_Process class
- Syntax
- Parameters
- Return value
- Remarks
- Examples
- Unable to terminate process error in Windows 10 [FIX]
- How do I fix the Unable to Terminate Process Error
- 1. Press the Alt + F4 Key
- 2. Switch to an Admin Account
- 3. Terminate the Process With Taskkill
- 4. Terminate the Process With WMIC
- 5. Check Out Alternative Task Managers
- How do I kill the process currently using a port on localhost in Windows?
- 21 Answers 21
TerminateProcess function (processthreadsapi.h)
Terminates the specified process and all of its threads.
Syntax
Parameters
A handle to the process to be terminated.
The handle must have the PROCESS_TERMINATE access right. For more information, see Process Security and Access Rights.
The exit code to be used by the process and threads terminated as a result of this call. Use the GetExitCodeProcess function to retrieve a process’s exit value. Use the GetExitCodeThread function to retrieve a thread’s exit value.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
The TerminateProcess function is used to unconditionally cause a process to exit. The state of global data maintained by dynamic-link libraries (DLLs) may be compromised if TerminateProcess is used rather than ExitProcess.
This function stops execution of all threads within the process and requests cancellation of all pending I/O. The terminated process cannot exit until all pending I/O has been completed or canceled. When a process terminates, its kernel object is not destroyed until all processes that have open handles to the process have released those handles.
When a process terminates itself, TerminateProcess stops execution of the calling thread and does not return. Otherwise, TerminateProcess is asynchronous; it initiates termination and returns immediately. If you need to be sure the process has terminated, call the WaitForSingleObject function with a handle to the process.
A process cannot prevent itself from being terminated.
After a process has terminated, call to TerminateProcess with open handles to the process fails with ERROR_ACCESS_DENIED (5) error code.
Terminate method of the Win32_Process class
The Terminate WMI class method terminates a process and all of its threads.
This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.
Syntax
Parameters
Exit code for the process and for all of the threads terminated as a result of this call.
Return value
Returns a value of 0 (zero) if the process was successfully terminated, and any other number to indicate an error. For additional error codes, see WMI Error Constants or WbemErrorEnum. For general HRESULT values, see System Error Codes.
Successful completion (0)
Access denied (2)
Insufficient privilege (3)
Unknown failure (8)
Path not found (9)
Invalid parameter (21)
Other (22 4294967295)
Remarks
Overview
Computer problems are often due to a process that is no longer working as expected. For example, the process might be leaking memory, or it might have stopped responding to user input. When problems such as these occur, the process must be terminated. Although this might seem like a simple enough task, terminating a process can be complicated by several factors:
- The process might be hung and therefore no longer responds to menu or keyboard commands for closing the application. This makes it all but impossible for the typical user to dismiss the application and terminate the process.
- The process might be orphaned. For example, a script might create an instance of Word and then exit without destroying that instance. In effect, Word remains running on the computer, even though no user interface is visible. Because there is no user interface, there are no menu or keyboard commands available to terminate the process.
- You might not know which process needs to be terminated. For example, you might want to terminate all programs that are exceeding a specified amount of memory.
- Because Task Manager allows you to terminate only those processes that you created, you might not be able to terminate a process, even if you are an administrator on the computer.
Scripts enable you to overcome all of these potential obstacles, providing you with considerable administrative control over your computers. For example, if you suspect users are playing games that have been prohibited in your organization, you can easily write a script to connect to each computer, identify whether the game is running, and immediately terminate the process.
Using the Terminate Method
You can terminate a process by:
- Terminating a process that is currently running. For example, you might need to terminate a diagnostic program running on a remote computer. If there is no way to control the application remotely, you can simply terminate the process for that application.
- Preventing a process from running in the first place. By continuously monitoring process creation on a computer, you can identify and instantly terminate any process as soon as it starts. This provides one method of ensuring that certain applications (such as programs that download large media files over the Internet) are never run on certain computers.
Group Policy can also be used to restrict the programs that run on a computer. However, Group Policy can restrict only the programs run using either the Start menu or Windows Explorer; it has no effect on programs started using other means, such as the command line. By contrast, WMI can prevent a process from running regardless of how the process was started.
Terminating a Process You Do Not Own
To terminate a process that you do not own, enable the SeDebugPrivilege privilege. In VBScript, you can enable this privilege with the following lines of code:
For more information about enabling this privilege in C++, see Enabling and Disabling Privileges in C++.
Examples
The Terminate running process on multiple servers PowerShell code sample on TechNet Gallery terminates a process running on a single or multiple computers.
The following VBScript sample terminates the process in which the application Diagnose.exe is currently running.
The following VBScript sample uses a temporary event consumer to terminate a process as soon as it starts.
Unable to terminate process error in Windows 10 [FIX]
Users can usually close the software and other processes by clicking Task Manger’s End task button in Windows. However, Task Manager doesn’t always terminate processes. Some users have stated that an “Unable to terminate process” error message window pops up when they try to terminate certain processes. The error message states: “The operation could not be completed. Access is denied.”
Task Manager doesn’t terminate the required process when that error arises. Consequently, users can’t close unresponsive software or other service processes with Task Manager when the “Unable to terminate process” error message pops up. Nevertheless, there are a few other ways that users can potentially terminate the process for an unresponsive program.
How do I fix the Unable to Terminate Process Error
1. Press the Alt + F4 Key
Alt + F4 is a handy keyboard shortcut for closing unresponsive programs. Try pressing the Alt + F4 hotkey to force quit an unresponsive program when you can’t close it with Task Manager. Then users might not need to select the program’s process in Task Manager.
2. Switch to an Admin Account
Some users might need elevated rights to quit certain Task Manager processes. Thus, those users will need to switch to an admin account before they can terminate the process. Users can switch to the built-in Windows 10 admin account as follows.
- Press the Windows key + S hotkey.
- Enter ‘cmd’ in the Type here to search box.
- Right-click Command Prompt to select its Run as administrator option.
- Enter ‘net user administrator /active:yes’ in the Prompt, and press the Return key.
- Thereafter, restart the desktop or laptop.
- Log in with the new admin account just set up.
3. Terminate the Process With Taskkill
There are a few Command Prompt commands that might terminate a process when Task Manager doesn’t. Users can try terminating the process with taskill instead. Open the Command Prompt as admin as outlined above.
Then enter ‘taskkill /im process-name /f’ in the Prompt’s window, and press the Return key. However, users will need to replace ‘process-name’ with the actual process name listed in Task Manager. To find the process detail, right-click the app or background process listed in Task Manager and select Go to details, which will open the tab shown in the snapshot directly below. Replace ‘process-name’ with the process listed on that tab.
4. Terminate the Process With WMIC
Alternatively, a Windows Management Instrument Console (WMIC) command might terminate the required process. Enter ‘wmic process where name=‘myprocessname.exe’ delete’ in an elevated Command Prompt, and press the Return key. Users will need to replace ‘myprocessname.exe’ with the actual process by checking the Details tab for it much the same as required for the taskill command.
5. Check Out Alternative Task Managers
There are numerous third-party Task Manager alternatives that might terminate the process for which the “Unable to terminate process” error occurs. Some third-party task manager utilities display more extensive system details and include more options. Process Hacker, System Explorer, and Process Explorer are among the more notable alternatives to Task Manager that might terminate the required process when TM doesn’t.
- To add System Explorer to Windows 10, click Download Now on the software’s website.
- Then launch SE’s installer to install the software, and open System Explorer’s window shown directly below.
- Select Processes on the left of the window.
- Then right-click the process and select End Process (or End Process Tree).
So, there’s more than one way to terminate software and services in Windows. When the “Unable to terminate process” error arises, try closing the required process with the Alt + F4 hotkey, Command Prompt commands, or third-party software as outlined above.
RELATED ARTICLES TO CHECK OUT:
How do I kill the process currently using a port on localhost in Windows?
How can I remove the current process/application which is already assigned to a port?
For example: localhost:8080
21 Answers 21
Step 1:
Open up cmd.exe (note: you may need to run it as an administrator, but this isn’t always necessary), then run the below command:
with the port number you want, but keep the colon)
The area circled in red shows the PID (process identifier). Locate the PID of the process that’s using the port you want.
Step 2:
Next, run the following command:
(No colon this time)
Lastly, you can check whether the operation succeeded or not by re-running the command in «Step 1». If it was successful you shouldn’t see any more search results for that port number.
Step 1 (same is in accepted answer written by KavinduWije):
Change in Step 2 to:
Note: taskkill is not working in some git bash terminal
With Windows 10 default tools:
Open Windows PowerShell as Administrator
Find PID (ProcessID) for port 8080:
Kill the zombie process:
where «77777» is your PID
If you are using GitBash
( /F forcefully terminates the process)
I know that is really old question, but found pretty easy to remember, fast command to kill app that are using port.
Requirements: npm@5.2.0^ version
You can also read more about kill-port here: https://www.npmjs.com/package/kill-port
Result for Windows Command Prompt
In my case, 8080 is the port I want to kill
And 18264 is the PID listening on port 8080
So the task you have to kill is the PID for that particular port
If you already know the port number, it will probably suffice to send a software termination signal to the process (SIGTERM):
In Windows PowerShell version 1 or later to stop a process on port 3000 type:
Stop-Process (,(netstat -ano | findstr :3000).split() | foreach <$[$.length-1]>) -Force
As suggested by @morganpdx here`s a more PowerShell-ish, better version:
Stop-Process -Id (Get-NetTCPConnection -LocalPort 3000).OwningProcess -Force
For use in command line:
For use in bat-file:
For Windows users, you can use the CurrPorts tool to kill ports under usage easily:
I was running zookeeper on Windows and wasn’t able to stop ZooKeeper running at 2181 port using zookeeper-stop.sh, so tried this double slash «//» method to taskkill. It worked
If you can use PowerShell on Windows you just need :
If you’re using Windows Terminal then the killing process might be little less tedious. I’ve been using windows terminal and kill PID works fine for me to kill processes on the port as the new Windows Terminal supports certain bash commands. For example: kill 13300
So, the complete process will look like this-
- Open Windows Terminal
- Type the following command to show processes running on the port you’re looking to kill processes. netstat -ano | findstr :PORT
- Type following to kill the process. kill PID
See when I typed the first command to list processes on the port it returned empty. That means all processes are killed now.
Update: kill is an alias for Stop-Process. Thanks, @FSCKur for letting us know.