如何在Windows-CE中杀死进程?
如何使用 C# 代码从终端 (Windows-CE 5.0) 终止进程 Windows\MyProcc.exe?
How can I kill process Windows\MyProcc.exe from my terminal (Windows-CE 5.0) using C# code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先通过给出正在运行的 exe 的名称找到进程并杀死它。使用 System.Diagnostics 命名空间。
First find the Process by giving the running exe's name and kill it. Use the System.Diagnostics namespace.
答案是您必须使用 toolhelp API。 MSDN 上有一个完整的示例,其中包括枚举进程和终止选定进程。
The answer is that you have to use the toolhelp APIs. There's a full example on MSDN that includes enumerating processes and killing a selected one.
代码项目正是您所寻找的。
我发现这个类对于终止 CE 中的进程非常有用。
http://www.codeproject.com/Articles /36841/Compact-Framework-Process-class-that-supports-full
A code project does exactly what you are looking for.
I found this class very usefull in Killing processes in CE.
http://www.codeproject.com/Articles/36841/Compact-Framework-Process-class-that-supports-full
找到进程后,您可以调用
Kill
命令。它位于 System.Diagnostics 中,并且在 .NET Compact Framework 中也受支持,请参阅此处:
Process.Kill Method
不幸的是,Process.GetProcess 似乎在 .NET CF 中不起作用,因此您应该使用另一个在杀死进程之前找到进程的方法,还有关于此的文章:
Compact Framework Process 类支持完全指定的文件路径
Once you have found your process you can call
Kill
command.it's in
System.Diagnostics
and supported in .NET Compact Framework as well, see here:Process.Kill Method
Unfortunately it looks like Process.GetProcess does not work in the .NET CF so you should use another way to find your process before killing it, there are also articles about this:
Compact Framework Process class that supports fully specified file paths
虽然 Compact Framework 没有为您提供列出所有进程的本机方法,但管理您创建的进程仍然很容易。
当您使用 Process.Start(name,arguments) 时,您只会返回一个布尔值。
这是因为该函数是一个共享函数,仅在您创建进程时使用,随后不关心它。它只是说明它是否创造了它。
您真正想要的是在启动可执行文件时获取进程描述符的副本,这意味着您需要创建 Process 的新实例,然后使用这些方法在这种情况下启动您的可执行文件。
因此,创建一个 Process 的新实例 - 将其命名为 P。现在这实际上是一个进程描述符 - 但它尚未连接到进程。
您会发现P公开了更多的属性。
设置可执行文件名称和参数(如果有的话)
P.StartInfo.FileName 和 P.StartInfo.Arguments
现在打电话
P.Start()
如果返回 True,则会将进程 ID 放入 P.Id 中,并维护指向您进程的实时链接。
P.Responding P.HasExited 和 P.ExitCode 中的值告诉您它是否仍在运行,如果不是,它是如何结束的以及当您厌倦了它时,可以使用无参数函数 P.Kill() 来结束它,因为这会结束与描述符关联的进程。
只需为您想要管理的每个进程创建一个新的 Process 实例,通过实例化变量而不是通用共享函数 Process.Start() 启动它,并将其保留为您的流程链接。
如果您只保留P.Id的值,您仍然可以使用共享函数Process.Kill(Id)来终止它,并且您可以使用Process. GetProcessById() 获取对引用它的描述符实例的引用。
这应该适用于任何语言。
Process 结构还包含名为 MainWindowHandle 的东西,它看起来像是创建任务的句柄。我从未检查过它是否匹配,但它确实被填充了。
因此,如果您想杀死“Windows\MyProcc.exe”,这很容易,只要它确实是“您的进程”,因为您可以通过返回其 ID 和描述符的方式创建它。
如果“Windows\MyProcc.exe”确实是“Windows\Someone-elses-Procc.exe”,那么它就不那么容易了,因为您需要知道 Id,并且没有简单的方法来获取它,除非它是您的。
希望这有帮助。
While the Compact Framework doesn't give you a native way to list all processes it is still easy to manage processes that you have created.
When you use Process.Start(name,arguments) you only get back a boolean.
This is because that function is a shared function that is just meant to be used when you create a process and subsequently don't care about it. It just says whether it created it or not.
What you really want is to get a copy of the process descriptor when you start your executable, and this means you need to create a new instance of Process and then use the methods in that instance to start your executable.
So, create a new instance of Process - call it P. This is now effectively a process descriptor - but it's not yet connected to a process.
You'll find that P exposes many more attributes.
Set your executable name and arguments (if you have any) in
P.StartInfo.FileName and P.StartInfo.Arguments
and now call
P.Start()
If it returns True this puts the process Id in P.Id for you and maintains a live link to your process.
The values in P.Responding P.HasExited and P.ExitCode tell you whether it is still running and, if not, how it ended and you can use the parameterless function P.Kill() to end it when you're fed up with it because that ends the process associated with the descriptor.
Simply create a new instance of Process for each process you want to manage, start it through the instantiating variable rather than the generic shared function Process.Start() and keep this as your link to the process.
If you only keep the value of P.Id you can still use the shared function Process.Kill(Id) to terminate it, and you can use Process.GetProcessById() to get a reference to the instance of the descriptor that refers to it.
This should work in any language.
The Process structure also contains something called MainWindowHandle which looks like it is the handle of the creating task. I've never checked to see if it matches, but it does get populated.
So, if you want to kill "Windows\MyProcc.exe" it is easy, provided that it really is "your process" because you can create it in a way that gives you back its Id and Descriptor.
If "Windows\MyProcc.exe" is really "Windows\Someone-elses-Procc.exe" then it's not so easy because you'd need to know the Id and there's no simple way to get it unless it's yours.
Hope this helps.