在程序运行过程中打开一个FrmList,然后在另一个窗体frmCustomer中关闭FrmList.
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName("FrmList");
ps[0].CloseMainWindow();
但运行后,提示错误:数组索引超出范围。
FrmList窗体确实是运行着的啊,有没有什么解决方法啊?
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName("FrmList");
ps[0].CloseMainWindow();
但运行后,提示错误:数组索引超出范围。
FrmList窗体确实是运行着的啊,有没有什么解决方法啊?
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);就可以了
Form frmList = Application.OpenForms["FrmList"];
if (frmList != null)
{
frmList.Close();
}
FrmList frmList = Application.OpenForms["FrmList"];
if (frmList != null)
{
frmList.Close();
}