我如何杀死我的应用程序创建的许多进程? [英] How can i kill many processess that is created by my application ?

查看:91
本文介绍了我如何杀死我的应用程序创建的许多进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个数组,用于保存我创建的所有进程.

This is an array that saves all my created processess.

static Process[] myProcess = new Process[10];
static private int cindex =0;



我提出了两种方法.一个用于创建进程,另一个用于杀死进程.



i made 2 methods. one for creating process and other to killprocess.

public void CreateProcess(string processName)
        {
            try
            {
                myProcess[cindex++] = Process.Start(processName);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "File cannot be open", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


public void KillProcess()
{
    for (int i = 0; i < cindex; i++)
    {
        myProcess[i].CloseMainWindow();
        myProcess[i].Close();
    }
}



如果我创建一个进程并通过上述函数将其杀死,则它工作正常,但如果我创建了1个以上的进程并调用此函数,则会出现以下运行时错误
没有与此对象相关的进程.

我正在创建的进程是iexplorer,mycomputer,taskmgr,winword等.



if i create one process and kill it by the above function, it is working fine but if i create more than 1 processess and call this function then it gives the following runtime error
No process is associated with this object.

Processess i am creating are iexplorer,mycomputer,taskmgr,winword etc

推荐答案

需要考虑的事情.由于将进程放入数组中,这意味着一旦进程myProcess [0]完成,就可能发生该错误.也许试试

Something to think about. Since you are putting the processes in an array that means that once process myProcess[0] finishes than possibly that error will happen. Maybe try

for (int i = 0; i < cindex; i++)
{
   
   if( !myProcess[i].HasExited )
   {
      
      myProcess[i].CloseMainWindow();
      myProcess[i].Close();
      
   }
   
}


最好使用以下列表:

This would be better served by a list:

private List<Process> processes = new List<Process>();

public void CreateProcess(string processName)
        {
            try
            {
                processes.Add(Process.Start(processName));
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "File cannot be open", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

public void KillProcess()
{
    foreach(Process p in processes)
    {
        myProcess[i].CloseMainWindow();
        myProcess[i].Close();        
    }
    p.Clear();
}



向每个请求发送终止请求后,您希望清除列表,这样您就不会尝试终止两次. (不过,可能需要执行一些更高级的处理以确保它确实消失了.)



You want to clear the list after you send kill requests to each one so you don''t try to kill them twice. (There may be some more advanced handling you need to do to make sure it actually does die, though.)


Kill停止该进程并关闭以释放与其相关的资源

Kill stops the process and close frees the resources associated with it

public void KillProcess()
{
    int i=0;
    while(i< MyProcess.Count)
    {
        if (!MyProcess[i].HasExited)
        {
            MyProcess[i].Kill();
            MyProcess[i].Close();

        }
        i++;
    }
    MyProcess.Clear();
}


这篇关于我如何杀死我的应用程序创建的许多进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆