在程序运行过程中打开一个FrmList,然后在另一个窗体frmCustomer中关闭FrmList.
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName("FrmList");
ps[0].CloseMainWindow();
但运行后,提示错误:数组索引超出范围。
FrmList窗体确实是运行着的啊,有没有什么解决方法啊?

解决方案 »

  1.   

      用KILL进程吧
       private void KillProcess(string processName)
       {
        Process process = new Process();
        try
        {
         foreach(Process thisproc in Process.GetProcessesByName(processName))
         {
          if(!thisproc.CloseMainWindow())
          {
           thisproc.Kill();
          }
         }
        }
        catch(Exception ex)
        {
         MessageBox.Show("杀死进程" + processName + "失败" +"\r\n" +"失败原因:" + ex.ToString());
        }
       }
    主函数调用KillProcess(FrmList);就可以了
      

  2.   

    operforms里就有名称为frmList和frmCustomer两个窗体,按楼上的方法循环kill的时候根本就进不了循环体,怎么回事
      

  3.   

    在frmCustomer中编写如下代码:
    Form frmList = Application.OpenForms["FrmList"];
    if (frmList != null)
    {
    frmList.Close();
    }
      

  4.   

    同意楼上
    FrmList frmList = Application.OpenForms["FrmList"];
    if (frmList != null)
    {
        frmList.Close();
    }