0

问题:如何在执行 bat 文件的过程中杀死一个进程,我可以让它杀死但继续显示方法的结果。

我正在将结果打印到控制台,但是,在 kill 之后无法获得停止的结果和执行路径。我正在运行 4 个进程。

internal class Program
{
    public static string line = string.Empty;

    private static void Main(string[] args)
    {

        LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(500);
        TaskFactory factory = new TaskFactory(lcts);


        for (var i = 0; i < 4; i++)
        {

            factory.StartNew(() =>
                                 {

                                     var pattern = string.Empty;
                                     ExecuteProcesses(pattern);
                                 }
                );
        }

    Console.ReadKey();
    }




public static void ExecuteProcesses(string pattern)
{

    var startInfo = new ProcessStartInfo();

    startInfo.FileName = @"C:\test.bat"; //pattern to replace string
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    startInfo.RedirectStandardOutput = true;
    startInfo.WorkingDirectory = @"C:\TEST\";
    startInfo.RedirectStandardError = true;
    //startInfo.UserName = "Administrator";


    using (Process batProcess = Process.Start(startInfo))
    {
        Console.WriteLine(string.Format("{0} | START | {1} | {2} | {3}",DateTime.Now, batProcess.StartTime, batProcess.Id, startInfo.FileName));

        //Console.WriteLine(batProcess.s);
        using (StreamReader reader = batProcess.StandardOutput)
        {

            if (!batProcess.WaitForExit(10000))
            {

                Console.WriteLine("Killing");
                try
                {
                    batProcess.CloseMainWindow();
                    batProcess.Kill();
                    batProcess.WaitForExit();
                    Console.WriteLine("Exited");
                    //Console.WriteLine("-->Killing Process ID {0}, Process exit code: {1},  End Time {2}\t",
                      //                batProcess.Id, batProcess.ExitCode, batProcess.ExitTime);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            ///Console.WriteLine("--> Process ID {0}, Start Time {1}\t", batProcess.Id, batProcess.StartTime);

                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    //list.Add(line);
                    Console.WriteLine(line);
                }

            //Write to console
            //Console.WriteLine("----------\r\n" + result + "----------\r\n");
            //Write to log
            //Common.WriteToLog(result);


            Console.WriteLine(result);

            Console.WriteLine(string.Format("{0} |  END  | {1} | {2} | {3}",DateTime.Now, batProcess.ExitTime, batProcess.Id, startInfo.FileName));
            //Console.WriteLine("-->Exit Process ID {0}, Process exit code: {1},  End Time {2}, Total Processing time {3}\t", batProcess.Id, batProcess.ExitCode, batProcess.ExitTime, batProcess.TotalProcessorTime);
        }
    }


    //if (!batProcess.WaitForExit(25000))
            //{
            //    Console.WriteLine("--> Process ID {0}, Process exit code: {1},  End Time {2}\t", batProcess.Id, batProcess.ExitCode, batProcess.ExitTime);
            //    Common.WriteToLog(batProcess.ExitCode.ToString());
            //}
            //else
            //{
            //    Console.WriteLine("Killing, Process exit code: {0}", batProcess.ExitCode);
            //    Process.GetCurrentProcess().Kill();

            //}

        }
    }
4

0 回答 0