How to kill a process in Windows Mobile?

天涯浪子 提交于 2019-12-08 11:04:32

问题


In a Windows Forms application I kill a process like this:

    Process[] ps = Process.GetProcessesByName("DataWedge");
    foreach (Process p in ps)
        p.Kill();

How can I do it in the Windows Mobile operating system?

(This sample doesn't work on Windows Mobile.)


回答1:


You need to enumerate the Processes running on the device in order to get it's process ID. Once you've got the processId you can just do:

Process process Process.GetProcessById(processId);
process.Kill();

Here's an article that deals with enumerating the processes, it also includes a kill example as well.




回答2:


Since the above article no longer exists I used the Wayback Machine to get the article to use the code. Below is how you would use the code from the article to enumerate through the processes and kill the one that you want:

List<ProcEntry> processes = new List<ProcEntry>();
ProcessEnumerator.Enumerate(ref processes);

foreach (ProcEntry proc in processes)
{
    if (proc.ExeName == "DataWedge.exe")
    {
        ProcessEnumerator.KillProcess(proc.ID);
    }
}



回答3:


There is a bug in this code that makes it always return an empty list.
First, add this constant :

private const uint TH32CS_SNAPNOHEAPS = 0x40000000;

Then call it like this :

IntPtr snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0 );


来源:https://stackoverflow.com/questions/1660087/how-to-kill-a-process-in-windows-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!