How force close all IE processes - with alert() on form_unload

旧街凉风 提交于 2019-12-11 04:36:36

问题


I wrote the codes below to close all IE (Internet Explorer) Processes.

private void Kill_IE()
{
    Process[] ps = Process.GetProcessesByName("IEXPLORE");

    foreach (Process p in ps)
    {
        p.Kill();
    }
}

The problem is some opened IE windows show an alert :

alert("do not close my web site"); //in JavaScript

or a

confirm_box("do you really want to exit?" , "YES|NO"); //in JavaScript

before exit(Unload_Form) and my program can't close those windows.
How can i ignore those alerts and force close those windows?


Edit :
The ways below can't close that browser with that annoying alert...

Show the black console :

Process.Start("taskkill", "/im iexplore.exe /f /t");

Hide the black Console :

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "taskkill.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "/f /t /im iexplore.exe";
Process.Start(startInfo);

Any Idea?


回答1:


I've tested this code and it works every time for me. I'm using IE11 and have opened both remote and local sites that generate alerts and the processes still close without an error.

I suggest wrapping this process in a try/catch to see if there any exceptions generated as I believe the fault lies elsewhere.

private void Kill_IE()
{
    Process[] ps = Process.GetProcessesByName("IEXPLORE");

    foreach (Process p in ps)
    {
        try
        {
            if (!p.HasExited)
            {
                p.Kill();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(String.Format("Unable to kill process {0}, exception: {1}", p.ToString(), ex.ToString()));
        }
    }
}


来源:https://stackoverflow.com/questions/31098717/how-force-close-all-ie-processes-with-alert-on-form-unload

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