停止进程 vs TaskKill [英] Stop-Process vs TaskKill

查看:49
本文介绍了停止进程 vs TaskKill的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自动刷新图标缓存,我发现 TaskKillStop-Process 之间存在有趣的区别.一般来说,我更喜欢使用本机 PowerShell 而不是从 PowerShell 启动的 DOS 命令行内容,所以我宁愿使用 Stop-Process -name:explorer 而不是 Start-Process TaskKill "/f/im explorer.exe" -NoNewWindow 以停止 Explorer.exe,以便 DB 文件不再使用中"并可被删除.但是,前者允许Explorer.exe立即重启,所以我需要删除的图标DB文件还在使用中,我无法删除它们.后者确实杀死了 Explorer.exe,稍后我必须使用 Start-Process.有没有办法使用 Stop-Process 来获得 TaskKill 行为,或者这是一种罕见的情况,即老式混杂也是唯一有效的方法?

I am automating an icon cache refresh, and I am seeing an interesting difference between TaskKill and Stop-Process. In general I prefer using native PowerShell over DOS command line stuff launched from PowerShell, so I would rather use Stop-Process -name:explorer over Start-Process TaskKill "/f /im explorer.exe" -NoNewWindow to stop Explorer.exe so that the DB files are no longer "in use" and can be deleted. However, the former allows Explorer.exe to restart instantly, so the icon DB files I need to delete are still in use and I can't delete them. The latter truly kills Explorer.exe and I have to use Start-Process later. Is there a way to get the TaskKill behavior using Stop-Process, or is this a rare situation where the old school kludge is also the only way that works?

推荐答案

我做了一些研究,你最好通过发送消息来结束浏览器.

I done some research and you should better end the explorer by sending a message.

使用此 WM_EXITEXPLORER (1460) 的消息,您可以告诉资源管理器关闭.

With a messaage espacially for this WM_EXITEXPLORER (1460), you can tell the explorer to be closed.

这里我的代码适用于 Windows 10:

Here my code working for windows 10:

$code = @'
[DllImport("user32.dll", EntryPoint = "PostMessage", CharSet =  CharSet.Unicode)] public static extern IntPtr PostMessage(IntPtr hWnd, int Msg, uint wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "FindWindowW", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
'@
$myAPI = Add-Type -MemberDefinition $code -Name myAPI -PassThru
$myAPI::PostMessage($myAPI::FindWindow("Shell_TrayWnd", $Null),1460,0,0)

Start-Sleep -Seconds 10

最好等待资源管理器窗口关闭,也许我明天会添加.现在 10 秒等待应该足以让 explorer.exe 优雅地结束.

It better to wait for the explorer windows to be closed, maybe I will add that tomorrow. For now 10 second wait should be enough for the explorer.exe to end graceful.

这比使用 kill 更好!

This is completly better than use the kill at all!

这篇关于停止进程 vs TaskKill的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆