如何在 Windows-CE 中杀死进程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7307998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:00:02  来源:igfitidea点击:

how to kill process in Windows-CE?

c#windowsprocesswindows-ce

提问by Gali

How can I kill process Windows\MyProcc.exe from my terminal (Windows-CE 5.0) using C# code?

如何使用 C# 代码从我的终端 (Windows-CE 5.0) 终止进程 Windows\MyProcc.exe?

回答by Sai Kalyan Kumar Akshinthala

First find the Process by giving the running exe's name and kill it. Use the System.Diagnostics namespace.

首先通过给出正在运行的 exe 的名称找到进程并杀死它。使用 System.Diagnostics 命名空间。

Process[] Prs = Process.GetProcessesById(RunninExe);
if (Prs.Length > 0)
{
      foreach (Process Prss in Prs)
      {
          Prss.Kill();
      }
}

回答by ctacke

The answer is that you have to use the toolhelp APIs. There's a full example on MSDNthat includes enumerating processes and killing a selected one.

答案是您必须使用工具帮助 API。MSDN 上有一个完整的示例,其中包括枚举进程和杀死选定的进程。

回答by CathalMF

A code project does exactly what you are looking for. I found this class very usefull in Killing processes in CE.

代码项目完全符合您的要求。我发现这个类在 CE 中的终止进程中非常有用。

http://www.codeproject.com/Articles/36841/Compact-Framework-Process-class-that-supports-full

http://www.codeproject.com/Articles/36841/Compact-Framework-Process-class-that-supports-full

    ProcessInfo[] list = ProcessCE.GetProcesses();

    foreach (ProcessInfo pinfo in list)
    {
        if (pinfo.FullPath.EndsWith("MyExe.exe"))
            pinfo.Kill();
    }

回答by Davide Piras

Once you have found your process you can call Killcommand.

找到进程后,您可以调用Kill命令。

it's in System.Diagnosticsand supported in .NET Compact Framework as well, see here:

System.Diagnostics也在 .NET Compact Framework中并受支持,请参见此处:

Process.Kill Method

Process.Kill 方法

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:

不幸的是,它看起来 Process.GetProcess 在 .NET CF 中不起作用,所以你应该使用另一种方法在杀死它之前找到你的进程,还有关于这个的文章:

Compact Framework Process class that supports fully specified file paths

支持完全指定文件路径的 Compact Framework Process 类

回答by Steve Douglas

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.

虽然 Compact Framework 没有为您提供列出所有进程的本机方式,但管理您创建的进程仍然很容易。

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.

当您使用Process.Start(name,arguments) 时,您只会得到一个布尔值。这是因为该函数是一个共享函数,仅在您创建进程时使用,随后不关心它。它只是说它是否创建了它。

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 instanceof Processand then use the methods in that instance to start your executable.

您真正想要的是在启动可执行文件时获取进程描述符的副本,这意味着您需要创建Process新实例,然后使用该实例中的方法来启动可执行文件。

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.

因此,创建一个Process的新实例- 将其称为P。这现在实际上是一个进程描述符 - 但它尚未连接到进程。

You'll find that Pexposes many more attributes.

你会发现P公开了更多的属性。

Set your executable name and arguments (if you have any) in P.StartInfo.FileNameand P.StartInfo.Argumentsand now call P.Start()

P.StartInfo.FileNameP.StartInfo.Arguments 中设置可执行文件名称和参数(如果有), 然后调用 P.Start()

If it returns True this puts the process Id in P.Idfor you and maintains a live link to your process.

如果它返回 True,这会将进程 Id 放在P.Id 中,并维护到您的进程的实时链接。

The values in P.RespondingP.HasExitedand P.ExitCodetell 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.

P.Responding P.HasExitedP.ExitCode 中的值告诉你它是否仍在运行,如果不是,它是如何结束的,当你厌倦时,你可以使用无参数函数P.Kill()来结束它因为那会结束与描述符相关的进程。

Simply create a new instance of Processfor 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.

只需为您要管理的每个流程创建一个新的Process实例,通过实例化变量而不是通用共享函数Process.Start()启动它,并将其作为流程的链接。

If you only keep the value of P.Idyou 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.

如果您只保留P.Id的值,您仍然可以使用共享函数Process.Kill(Id)来终止它,并且您可以使用Process.GetProcessById()来获取对引用它的描述符实例的引用.

This should work in any language.

这应该适用于任何语言。

The Processstructure also contains something called MainWindowHandlewhich looks like it is the handle of the creating task. I've never checked to see if it matches, but it does get populated.

工艺结构还含有一种叫MainWindowHandle这看起来是创建任务的句柄。我从来没有检查过它是否匹配,但它确实被填充了。

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.

因此,如果您想杀死“Windows\MyProcc.exe”,这很容易,前提是它确实是“您的进程”,因为您可以通过一种方式创建它,从而返回其 ID 和描述符。

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.

如果“Windows\MyProcc.exe”确实是“Windows\Someone-elses-Procc.exe”,那么它就不是那么容易了,因为您需要知道 Id 并且没有简单的方法可以获取它,除非它是您的。

Hope this helps.

希望这可以帮助。