一定时间后杀死进程+ C#

Dev*_*per 11 .net process c#-2.0

如何在说出2或3分钟之后查看以下代码:

 class Program
{
    static void Main(string[] args)
    {

        try
        {
            //declare new process and name it p1
            Process p1 = Process.Start("iexplore", "http://www.google.com");
            //get starting time of process
            DateTime startingTime = p1.StartTime;
            Console.WriteLine(startingTime);
            //add a minute to startingTime
            DateTime endTime = startingTime.AddMinutes(1); 
            //I don't know how to kill process after certain time
            //code below don't work, How Do I kill this process after a minute or 2
            p1.Kill(startingTime.AddMinutes(2));                
            Console.ReadLine();


        }
        catch (Exception ex)
        {

            Console.WriteLine("Problem with Process:{0}", ex.Message);
        }



    }
}
Run Code Online (Sandbox Code Playgroud)

所以我希望IE窗口在2分钟后关闭

Jon*_*eet 24

使用Process.WaitForExit超时两分钟,然后Process.Kill如果WaitForExit返回则调用false.

(您可能还要考虑调用CloseMainWindow而不是Kill根据您的情况 - 或者至少先尝试调用,以便为进程提供更多有序关闭的机会.)