开发者

how to kill process in Windows-CE?

开发者 https://www.devze.com 2023-04-03 05:52 出处:网络
How can I kill process Windows\\MyProcc.exe from 开发者_开发技巧my terminal (Windows-CE 5.0) using C# code?First find the Process by giving the running exe\'s name and kill it. Use the System.Diagnost

How can I kill process Windows\MyProcc.exe from 开发者_开发技巧my terminal (Windows-CE 5.0) using C# code?


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

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


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.


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

    ProcessInfo[] list = ProcessCE.GetProcesses();

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号