任务工厂对象杀死方法c#

时间:2013-07-26 15:46:25

标签: c# object task factory kill

问题:如何在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();

            //}

        }
    }

0 个答案:

没有答案