我正试图杀死notepad.exe
使用此函数在 Windows 上进行处理:
import thread, wmi, os
print 'CMD: Kill command called'
def kill():
c = wmi.WMI ()
Commands=['notepad.exe']
if Commands[0]!='All':
print 'CMD: Killing: ',Commands[0]
for process in c.Win32_Process ():
if process.Name==Commands[0]:
process.Terminate()
else:
print 'CMD: trying to kill all processes'
for process in c.Win32_Process ():
if process.executablepath!=inspect.getfile(inspect.currentframe()):
try:
process.Terminate()
except:
print 'CMD: Unable to kill: ',proc.name
kill() #Works
thread.start_new_thread( kill, () ) #Not working
当我像这样调用该函数时,它就像一个魅力:
kill()
但是当在新线程中运行该函数时,它崩溃了,我不知道为什么。
import thread, wmi, os
import pythoncom
print 'CMD: Kill command called'
def kill():
pythoncom.CoInitialize()
. . .
在线程中运行 Windows 函数可能很棘手,因为它通常涉及 COM 对象。使用pythoncom.CoInitialize()
通常允许你这样做。另外,您可能还想看看线程 http://docs.python.org/2/library/threading.html图书馆。它比线程更容易处理。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)