windows - 如何使用 nim 语言在 Windows 上处理 taskkilll 命令?
问题描述
有没有办法处理Windows在没有选项的进程上使用WM_CLOSE
时发送的事件?ffmpeg 并非旨在捕获此事件,我想编写一个可以管理 ffmpeg 进程并告诉它在收到事件时正常关闭的程序。我对 Windows API 非常陌生,所以我不知道从哪里开始。taskkill
/F
nim
WM_CLOSE
nim
这是一个显示 ffmpeg https://imgur.com/a/JxGjiiX问题的 gif
如果我使用该选项强行终止 ffmpeg 进程,/F
它将在编码/录制期间以文件损坏告终。
这是我用来测试此功能的代码:
import os, times, strutils
let t = epochTime()
proc handler() {.noconv.} =
echo "Program has run for ", formatFloat(epochTime() - t, precision = 0), " seconds."
quit(0)
setControlCHook(handler)
while true:
sleep 500
这不会捕获WM_CLOSE
由taskkill
.
我怎样才能让我的nim
程序捕捉到WM_CLOSE
事件?
解决方案
问题
如果没有与进程关联的窗口句柄,则在没有标志的情况下使用它时无法捕获发送的WM_CLOSE
事件。taskkill
/F
taskkill
,当不带标志使用时,向与您调用时使用的 pid 关联的窗口句柄/F
发送WM_CLOSE
或消息WM_QUIT
taskkill
请注意,我在 Windows 10 上,所以我必须 catchWM_CLOSE
而不是WM_QUIT
您可以通过打开命令提示符并运行 ffmpeg 自己测试问题,然后尝试使用taskkill /PID {pid}
或taskkill /IM ffmpeg.exe
. ffmpeg 将继续编码/记录并taskkill
告诉你它成功地终止了进程。
ffmpeg.exe -rtbufsize 150M -f gdigrab -framerate 30 -offset_x 448 -offset_y 240 -video_size 1024x600 -draw_mouse 1 -show_region 1 -i desktop -r 30 -preset ultrafast -tune zerolatency -movflags +faststart output.mp4
这是一个尝试关闭 ffmpeg 进程的演示taskkill
。的默认行为taskkill
是未处理的,因为 ffmpeg 不会创建可以将WM_CLOSE
事件发送给它的窗口句柄。
^ 我使用我的自定义 ffmpeg 包装器来捕捉WM_CLOSE
事件并优雅地关闭 ffmpeg 以记录此剪辑。
解决方案
将窗口与 Windows 上的任何可执行文件关联的一种方法是使用 start 命令:
start "Your window title" program.exe -option1 -option2
然后您可以使用taskkill
发送WM_CLOSE
事件,该事件可以被拦截,但 ffmpeg 实际上并没有捕获该事件,因为它最初并不是为了捕获它而设计的。
为了解决这个问题nim
,您可以使用 3 个附加模块的组合来创建窗口句柄、启动/监视进程、拦截WM_CLOSE
事件并将“q”写入 ffmpeg 进程的stdout
.
在其他版本的 Windows 中taskkill
发送WM_QUIT
事件,因此您可能也需要处理它。
wNim
用;创建窗口句柄 winim
仅用于访问用于事件处理程序的WM_CLOSE
常量。wNim
import os
import wnim
import winim
import nimpy
# Put python3.8.dll into following folder. It is required by nimpy
# C:/Users/username/AppData/Local/Microsoft/WindowsApps
let app = App()
let frame = Frame(title="ffmpeg", size=(400, 300)) # Size doesn't matter because we never show the frame.
使用nimpy加载python3.8.dll
文件,以便您可以使用python 中的模块。(比内置的多处理库恕我直言更易于使用)subprocess
nim
# This is used to check if ffmpeg has been launched
var running_process = false
# The process must be a PyObject, not a Process object from nim
var the_proc: PyObject
# Get reference to the subprocess module
let subprocess = pyImport("subprocess")
# Get reference to python builtin modules
let py = pyBuiltinsModule()
# Get a reference to the subprocess PIPE
let pipe = subprocess.PIPE
创建事件处理程序。这些将处理应用程序的首次启动和WM_CLOSE
Windows 中的事件。
# We catch the WM_MOVE event and start the process.
# We force the event to trigger when we center
# the frame and start the app.
frame.connect(WM_MOVE) do (event: wEvent):
# Make sure we only start 1 process.
if running_process == false:
running_process = true
# Create command and start the process.
var command_str: string = paramStr(1)
for i in 2..paramCount():
command_str = command_str & " " & paramStr(i)
# Use python subprocess module to execute command and set the stdin to the pipe.
the_proc = subprocess.Popen(command_str, stdin=pipe)
# When WM_CLOSE is triggered from taskkill, write the utf-8 encoded "q"
# to the ffmpeg process
frame.connect(WM_CLOSE) do (event: wEvent):
var command = "q"
# Make sure objects are all safe to use
# They should by python objects. Call the
# standard library to create the python objects.
var pycommand = py.str.encode(py.str(command), "utf-8")
# With a python process you call `communicate` on
# the process when you want it to wait to complete.
# Since ffmpeg just needs the "q" character we send
# the utf-8 encoded "q" with the `input` keyword
discard the_proc.communicate(input=pycommand)
sleep(1000)
# Get rid of process and quit.
discard the_proc.terminate()
quit(0)
然后所有需要做的就是使框架居中并启动应用程序。
frame.center()
app.mainLoop()
请记住,这使用了 python 标准库,因此您需要将 python dll 放入以下文件夹nimpy
以获取访问权限。
C:/Users/username/AppData/Local/Microsoft/WindowsApps
如果您碰巧遇到相同的问题,这里有一个存储库,其中包含您需要的一切:
推荐阅读
- c# - 发件人对象 - с#
- docker - Kubeflow 是否可以将本地文件夹挂载到多个容器操作?
- ios - 在 ios 中为特定条件隐藏故事板中设置的图像
- javascript - 使用 JS /g 函数替换字符显示未定义
- java - 什么是查找仅包含数字的计数的方法
- php - Opencart v1.5.6.4 - PHP 致命错误:'函数名必须是字符串'
- ios - Swift 布尔值不起作用:无法将“Bool”类型的值分配给“BOOL”类型(又名“Int32”)
- c# - 通过 c# 中的 api 从共享点获取文件/文件夹
- hapijs - 修复 Hapi 版本 19.0.3 错误 415 不支持的媒体类型上传文件与 multipart/form-data
- python - Paragram_300_SL999 Word Embeddings 文件是否损坏?